<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/Frog_32px_1177822_easyicon.net.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/Frog_32px_1177822_easyicon.net.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/Frog_16px_1177822_easyicon.net.ico">
  <link rel="mask-icon" href="/images/Frog_32px_1177822_easyicon.net.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"hxy1997.xyz","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":true,"nav":null,"activeClass":"valine"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"输入关键字","hits_empty":"没有找到与「${query}」相关搜索","hits_stats":"${hits} 条相关记录，共耗时 ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.json"};
  </script>

  <meta name="description" content="在今年寒假改完论文后，做了一个决定，就是在条件允许的情况下参加力扣周赛。只是做自己想做的事，语言用回C++吧。希望自己能够在其中得到成长。">
<meta property="og:type" content="article">
<meta property="og:title" content="力扣周赛301-">
<meta property="og:url" content="https://hxy1997.xyz/2022/07/19/%E5%8A%9B%E6%89%A3%E5%91%A8%E8%B5%9B301-/index.html">
<meta property="og:site_name" content="hxy的博客">
<meta property="og:description" content="在今年寒假改完论文后，做了一个决定，就是在条件允许的情况下参加力扣周赛。只是做自己想做的事，语言用回C++吧。希望自己能够在其中得到成长。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220710181033.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220718090335.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220724222818.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220724224120.jpeg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220724224146.jpeg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220801091228.png">
<meta property="og:image" content="https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-2.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220802091148.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220802091635.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220802091646.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220808075451.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220808075740.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220808075800.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220821221731.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220821221819.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220821221838.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220821222012.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220821222024.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220821223553.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220821224643.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220821224658.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220828220724.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220828223454.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220904223705.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220919090522.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220919090436.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220922224147.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220922224205.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220925135205.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220925142712.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220925142741.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220925223608.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20221002154624.png">
<meta property="og:image" content="https://assets.leetcode.com/uploads/2022/08/21/img.jpg">
<meta property="og:image" content="https://assets.leetcode.com/uploads/2022/08/21/1.jpg">
<meta property="og:image" content="https://assets.leetcode.com/uploads/2022/08/21/2.jpg">
<meta property="article:published_time" content="2022-07-19T10:00:00.000Z">
<meta property="article:modified_time" content="2022-10-02T08:38:09.148Z">
<meta property="article:author" content="hxy">
<meta property="article:tag" content="刷题">
<meta property="article:tag" content="力扣">
<meta property="article:tag" content="周赛">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220710181033.png">

<link rel="canonical" href="https://hxy1997.xyz/2022/07/19/%E5%8A%9B%E6%89%A3%E5%91%A8%E8%B5%9B301-/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>力扣周赛301- | hxy的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="hxy的博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">hxy的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Mia san Mia!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/huxingyi1997" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://hxy1997.xyz/2022/07/19/%E5%8A%9B%E6%89%A3%E5%91%A8%E8%B5%9B301-/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/Robben.gif">
      <meta itemprop="name" content="hxy">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="hxy的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          力扣周赛301-
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-07-19 18:00:00" itemprop="dateCreated datePublished" datetime="2022-07-19T18:00:00+08:00">2022-07-19</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-10-02 16:38:09" itemprop="dateModified" datetime="2022-10-02T16:38:09+08:00">2022-10-02</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%88%B7%E9%A2%98/" itemprop="url" rel="index"><span itemprop="name">刷题</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="热度" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">热度：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2022/07/19/%E5%8A%9B%E6%89%A3%E5%91%A8%E8%B5%9B301-/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2022/07/19/%E5%8A%9B%E6%89%A3%E5%91%A8%E8%B5%9B301-/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>在今年寒假改完论文后，做了一个决定，就是在条件允许的情况下参加力扣周赛。只是做自己想做的事，语言用回C++吧。希望自己能够在其中得到成长。</p>
<span id="more"></span>

<h2 id="Weekly-Contest-301"><a href="#Weekly-Contest-301" class="headerlink" title="Weekly Contest 301"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-301/">Weekly Contest 301</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220710181033.png" alt="image-20220710174532073"></p>
<h3 id="2335-Minimum-Amount-of-Time-to-Fill-Cups"><a href="#2335-Minimum-Amount-of-Time-to-Fill-Cups" class="headerlink" title="2335. Minimum Amount of Time to Fill Cups"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups/">2335. Minimum Amount of Time to Fill Cups</a></h3><p>You have a water dispenser that can dispense cold, warm, and hot water. Every second, you can either fill up <code>2</code> cups with <strong>different</strong> types of water, or <code>1</code> cup of any type of water.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>amount</code> of length <code>3</code> where <code>amount[0]</code>, <code>amount[1]</code>, and <code>amount[2]</code> denote the number of cold, warm, and hot water cups you need to fill respectively. Return <em>the <strong>minimum</strong> number of seconds needed to fill up all the cups</em>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: amount &#x3D; [1,4,2]</span><br><span class="line">Output: 4</span><br><span class="line">Explanation: One way to fill up the cups is:</span><br><span class="line">Second 1: Fill up a cold cup and a warm cup.</span><br><span class="line">Second 2: Fill up a warm cup and a hot cup.</span><br><span class="line">Second 3: Fill up a warm cup and a hot cup.</span><br><span class="line">Second 4: Fill up a warm cup.</span><br><span class="line">It can be proven that 4 is the minimum number of seconds needed.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Input: amount &#x3D; [5,4,4]</span><br><span class="line">Output: 7</span><br><span class="line">Explanation: One way to fill up the cups is:</span><br><span class="line">Second 1: Fill up a cold cup, and a hot cup.</span><br><span class="line">Second 2: Fill up a cold cup, and a warm cup.</span><br><span class="line">Second 3: Fill up a cold cup, and a warm cup.</span><br><span class="line">Second 4: Fill up a warm cup, and a hot cup.</span><br><span class="line">Second 5: Fill up a cold cup, and a hot cup.</span><br><span class="line">Second 6: Fill up a cold cup, and a warm cup.</span><br><span class="line">Second 7: Fill up a hot cup.</span><br></pre></td></tr></table></figure>

<p><strong>Example 3:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: amount &#x3D; [5,0,0]</span><br><span class="line">Output: 5</span><br><span class="line">Explanation: Every second, we fill up a cold cup.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>amount.length == 3</code></li>
<li><code>0 &lt;= amount[i] &lt;= 100</code></li>
</ul>
<p>Easy，这题我理了很久才想明白三个数的关系，静下心来，不要被自己的第一想法所左右</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">fillCups</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; amount)</span> </span>&#123;</span><br><span class="line">        sort(amount.begin(), amount.end());</span><br><span class="line">        <span class="keyword">int</span> res = amount[<span class="number">1</span>] - amount[<span class="number">0</span>];</span><br><span class="line">        amount[<span class="number">1</span>] -= res;</span><br><span class="line">        amount[<span class="number">2</span>] -= res;</span><br><span class="line">        <span class="comment">// cout&lt;&lt;amount[2]&lt;&lt;endl;</span></span><br><span class="line">        </span><br><span class="line">        <span class="keyword">int</span> k = amount[<span class="number">2</span>] / <span class="number">2</span>, m = amount[<span class="number">2</span>] % <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">if</span> (amount[<span class="number">0</span>] &lt; k) &#123;</span><br><span class="line">            <span class="keyword">return</span> amount[<span class="number">2</span>] + res;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (amount[<span class="number">0</span>] == k) &#123;</span><br><span class="line">            <span class="keyword">return</span> res + <span class="number">2</span> * k + m;</span><br><span class="line">            </span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            amount[<span class="number">0</span>] -= (k + m);</span><br><span class="line">            amount[<span class="number">1</span>] -= k;</span><br><span class="line">            <span class="comment">// cout&lt;&lt;res&lt;&lt;k&lt;&lt;m&lt;&lt;amount[1];</span></span><br><span class="line">            <span class="keyword">return</span> res + <span class="number">2</span> * k + m + amount[<span class="number">1</span>];</span><br><span class="line">            </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>其实很简单的问题</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">fillCups</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; amount)</span> </span>&#123;</span><br><span class="line">       <span class="keyword">int</span> mx = <span class="number">0</span>, sum = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span>&amp; num: amount) &#123;</span><br><span class="line">            mx = max(num, mx);</span><br><span class="line">            sum += num;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> max(mx, (sum + <span class="number">1</span>) / <span class="number">2</span>); </span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2336-Smallest-Number-in-Infinite-Set"><a href="#2336-Smallest-Number-in-Infinite-Set" class="headerlink" title="2336. Smallest Number in Infinite Set"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/smallest-number-in-infinite-set/">2336. Smallest Number in Infinite Set</a></h3><p>You have a set which contains all positive integers <code>[1, 2, 3, 4, 5, ...]</code>.</p>
<p>Implement the <code>SmallestInfiniteSet</code> class:</p>
<ul>
<li><code>SmallestInfiniteSet()</code> Initializes the <strong>SmallestInfiniteSet</strong> object to contain <strong>all</strong> positive integers.</li>
<li><code>int popSmallest()</code> <strong>Removes</strong> and returns the smallest integer contained in the infinite set.</li>
<li><code>void addBack(int num)</code> <strong>Adds</strong> a positive integer <code>num</code> back into the infinite set, if it is <strong>not</strong> already in the infinite set.</li>
</ul>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">Input</span><br><span class="line">[&quot;SmallestInfiniteSet&quot;, &quot;addBack&quot;, &quot;popSmallest&quot;, &quot;popSmallest&quot;, &quot;popSmallest&quot;, &quot;addBack&quot;, &quot;popSmallest&quot;, &quot;popSmallest&quot;, &quot;popSmallest&quot;]</span><br><span class="line">[[], [2], [], [], [], [1], [], [], []]</span><br><span class="line">Output</span><br><span class="line">[null, null, 1, 2, 3, null, 1, 4, 5]</span><br><span class="line"></span><br><span class="line">Explanation</span><br><span class="line">SmallestInfiniteSet smallestInfiniteSet &#x3D; new SmallestInfiniteSet();</span><br><span class="line">smallestInfiniteSet.addBack(2);    &#x2F;&#x2F; 2 is already in the set, so no change is made.</span><br><span class="line">smallestInfiniteSet.popSmallest(); &#x2F;&#x2F; return 1, since 1 is the smallest number, and remove it from the set.</span><br><span class="line">smallestInfiniteSet.popSmallest(); &#x2F;&#x2F; return 2, and remove it from the set.</span><br><span class="line">smallestInfiniteSet.popSmallest(); &#x2F;&#x2F; return 3, and remove it from the set.</span><br><span class="line">smallestInfiniteSet.addBack(1);    &#x2F;&#x2F; 1 is added back to the set.</span><br><span class="line">smallestInfiniteSet.popSmallest(); &#x2F;&#x2F; return 1, since 1 was added back to the set and</span><br><span class="line">                                   &#x2F;&#x2F; is the smallest number, and remove it from the set.</span><br><span class="line">smallestInfiniteSet.popSmallest(); &#x2F;&#x2F; return 4, and remove it from the set.</span><br><span class="line">smallestInfiniteSet.popSmallest(); &#x2F;&#x2F; return 5, and remove it from the set.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= num &lt;= 1000</code></li>
<li>At most <code>1000</code> calls will be made <strong>in total</strong> to <code>popSmallest</code> and <code>addBack</code>.</li>
</ul>
<p>Medium，我是使用栈</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SmallestInfiniteSet</span> &#123;</span></span><br><span class="line"><span class="built_in">stack</span>&lt;<span class="keyword">int</span>&gt; stk;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    SmallestInfiniteSet() &#123;</span><br><span class="line">        stk.push(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">popSmallest</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> res = stk.top();</span><br><span class="line">        stk.pop();</span><br><span class="line">        <span class="keyword">if</span> (stk.size() == <span class="number">0</span>) stk.push(res + <span class="number">1</span>);</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">addBack</span><span class="params">(<span class="keyword">int</span> num)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">stack</span>&lt;<span class="keyword">int</span>&gt; tmp;</span><br><span class="line">        <span class="keyword">while</span> (stk.size() &amp;&amp; stk.top() &lt; num) &#123;</span><br><span class="line">            tmp.push(stk.top());</span><br><span class="line">            stk.pop();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (stk.size() &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (stk.top() &gt; num) &#123;</span><br><span class="line">                stk.push(num);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">while</span> (tmp.size()) &#123;</span><br><span class="line">            stk.push(tmp.top());</span><br><span class="line">            tmp.pop();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Your SmallestInfiniteSet object will be instantiated and called as such:</span></span><br><span class="line"><span class="comment"> * SmallestInfiniteSet* obj = new SmallestInfiniteSet();</span></span><br><span class="line"><span class="comment"> * int param_1 = obj-&gt;popSmallest();</span></span><br><span class="line"><span class="comment"> * obj-&gt;addBack(num);</span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>

<p>或者用优先队列/set也可以解决，下面是使用priority_queueQQ</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SmallestInfiniteSet</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">priority_queue</span>&lt;<span class="keyword">int</span>, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;, greater&lt;<span class="keyword">int</span>&gt;&gt;q;</span><br><span class="line">    <span class="built_in">unordered_map</span>&lt;<span class="keyword">int</span>, <span class="keyword">bool</span>&gt;m;</span><br><span class="line">    SmallestInfiniteSet() &#123;</span><br><span class="line">        <span class="comment">// 插入1000个数到容器中，并更新map</span></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= <span class="number">1000</span>; i++) &#123;</span><br><span class="line">            q.push(i);</span><br><span class="line">            m[i] = <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">popSmallest</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> a = q.top();</span><br><span class="line">        q.pop();</span><br><span class="line">        m[a] = <span class="literal">false</span>; <span class="comment">// map记录这个数字为空</span></span><br><span class="line">        <span class="keyword">return</span> a;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">addBack</span><span class="params">(<span class="keyword">int</span> num)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 如果字典存在这个数字，但为空，插入</span></span><br><span class="line">        <span class="keyword">if</span> (m.count(num)) &#123; </span><br><span class="line">            <span class="keyword">if</span>(m[num] == <span class="literal">false</span>) q.push(num);</span><br><span class="line">            m[num]=<span class="literal">true</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123; <span class="comment">// 容器没这个数字，直接插入</span></span><br><span class="line">            q.push(num);</span><br><span class="line">            m[num] = <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Your SmallestInfiniteSet object will be instantiated and called as such:</span></span><br><span class="line"><span class="comment"> * SmallestInfiniteSet* obj = new SmallestInfiniteSet();</span></span><br><span class="line"><span class="comment"> * int param_1 = obj-&gt;popSmallest();</span></span><br><span class="line"><span class="comment"> * obj-&gt;addBack(num);</span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>

<p>使用set</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SmallestInfiniteSet</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">set</span>&lt;<span class="keyword">int</span>&gt;s;</span><br><span class="line">    SmallestInfiniteSet() &#123;</span><br><span class="line">        s.clear();</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= <span class="number">1010</span>; i++) s.insert(i);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">popSmallest</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> x = *s.begin();</span><br><span class="line">        s.erase(s.begin());</span><br><span class="line">        <span class="keyword">return</span> x;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">addBack</span><span class="params">(<span class="keyword">int</span> num)</span> </span>&#123;</span><br><span class="line">        s.insert(num);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Your SmallestInfiniteSet object will be instantiated and called as such:</span></span><br><span class="line"><span class="comment"> * SmallestInfiniteSet* obj = new SmallestInfiniteSet();</span></span><br><span class="line"><span class="comment"> * int param_1 = obj-&gt;popSmallest();</span></span><br><span class="line"><span class="comment"> * obj-&gt;addBack(num);</span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>



<h3 id="2337-Move-Pieces-to-Obtain-a-String"><a href="#2337-Move-Pieces-to-Obtain-a-String" class="headerlink" title="2337. Move Pieces to Obtain a String"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/move-pieces-to-obtain-a-string/">2337. Move Pieces to Obtain a String</a></h3><p>You are given two strings <code>start</code> and <code>target</code>, both of length <code>n</code>. Each string consists <strong>only</strong> of the characters <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code>, and <code>&#39;_&#39;</code> where:</p>
<ul>
<li>The characters <code>&#39;L&#39;</code> and <code>&#39;R&#39;</code> represent pieces, where a piece <code>&#39;L&#39;</code> can move to the <strong>left</strong> only if there is a <strong>blank</strong> space directly to its left, and a piece <code>&#39;R&#39;</code> can move to the <strong>right</strong> only if there is a <strong>blank</strong> space directly to its right.</li>
<li>The character <code>&#39;_&#39;</code> represents a blank space that can be occupied by <strong>any</strong> of the <code>&#39;L&#39;</code> or <code>&#39;R&#39;</code> pieces.</li>
</ul>
<p>Return <code>true</code> <em>if it is possible to obtain the string</em> <code>target</code> <em>by moving the pieces of the string</em> <code>start</code> *<strong>any</strong> number of times*. Otherwise, return <code>false</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: start &#x3D; &quot;_L__R__R_&quot;, target &#x3D; &quot;L______RR&quot;</span><br><span class="line">Output: true</span><br><span class="line">Explanation: We can obtain the string target from start by doing the following moves:</span><br><span class="line">- Move the first piece one step to the left, start becomes equal to &quot;L___R__R_&quot;.</span><br><span class="line">- Move the last piece one step to the right, start becomes equal to &quot;L___R___R&quot;.</span><br><span class="line">- Move the second piece three steps to the right, start becomes equal to &quot;L______RR&quot;.</span><br><span class="line">Since it is possible to get the string target from start, we return true.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: start &#x3D; &quot;R_L_&quot;, target &#x3D; &quot;__LR&quot;</span><br><span class="line">Output: false</span><br><span class="line">Explanation: The &#39;R&#39; piece in the string start can move one step to the right to obtain &quot;_RL_&quot;.</span><br><span class="line">After that, no pieces can move anymore, so it is impossible to obtain the string target from start.</span><br></pre></td></tr></table></figure>

<p><strong>Example 3:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: start &#x3D; &quot;_R&quot;, target &#x3D; &quot;R_&quot;</span><br><span class="line">Output: false</span><br><span class="line">Explanation: The piece in the string start can move only to the right, so it is impossible to obtain the string target from start.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == start.length == target.length</code></li>
<li><code>1 &lt;= n &lt;= 105</code></li>
<li><code>start</code> and <code>target</code> consist of the characters <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code>, and <code>&#39;_&#39;</code>.</li>
</ul>
<p>Medium，L和R数量一致，每个L和R相对位置一致，L只能向左，R只能向右</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">canChange</span><span class="params">(<span class="built_in">string</span> start, <span class="built_in">string</span> target)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> lNum = <span class="number">0</span>, rNum = <span class="number">0</span>;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; lPos, rPos;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; lRight, rLeft;</span><br><span class="line">        <span class="keyword">int</span> n = start.size();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (start[i] == <span class="string">&#x27;L&#x27;</span>) &#123;</span><br><span class="line">                lPos.push_back(i);</span><br><span class="line">                lRight.push_back(rNum);</span><br><span class="line">                lNum++;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (start[i] == <span class="string">&#x27;R&#x27;</span>) &#123;</span><br><span class="line">                rPos.push_back(i);</span><br><span class="line">                rLeft.push_back(lNum);</span><br><span class="line">                rNum++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        lNum = <span class="number">0</span>, rNum = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (target[i] == <span class="string">&#x27;L&#x27;</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (lNum &gt;= lPos.size()) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">                <span class="keyword">if</span> (i &gt; lPos[lNum]) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">                <span class="keyword">if</span> (lRight[lNum] != rNum) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">                lNum++;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (target[i] == <span class="string">&#x27;R&#x27;</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (rNum &gt;= rPos.size()) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">                <span class="keyword">if</span> (i &lt; rPos[rNum]) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">                <span class="keyword">if</span> (rLeft[rNum] != lNum) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">                rNum++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (lNum != lPos.size() || rNum != rPos.size()) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>双指针遍历2个字符串</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">canChange</span><span class="params">(<span class="built_in">string</span> start, <span class="built_in">string</span> target)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> i = <span class="number">0</span>, j = <span class="number">0</span>, n = start.length();</span><br><span class="line">        <span class="keyword">while</span>(i &lt; n &amp;&amp; j &lt; n)&#123;</span><br><span class="line">            <span class="keyword">while</span>(i &lt; n &amp;&amp; start[i++] == <span class="string">&#x27;_&#x27;</span>);</span><br><span class="line">            <span class="keyword">while</span>(j &lt; n &amp;&amp; target[j++] == <span class="string">&#x27;_&#x27;</span>);</span><br><span class="line">            i--;</span><br><span class="line">            j--;</span><br><span class="line">            <span class="keyword">if</span> (start[i] != target[j]) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            <span class="keyword">if</span> (start[i] == <span class="string">&#x27;L&#x27;</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (i &lt; j) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (start[i] == <span class="string">&#x27;R&#x27;</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (i &gt; j) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            i++;</span><br><span class="line">            j++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2338-Count-the-Number-of-Ideal-Arrays"><a href="#Failed-2338-Count-the-Number-of-Ideal-Arrays" class="headerlink" title="Failed: 2338. Count the Number of Ideal Arrays"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/count-the-number-of-ideal-arrays/">2338. Count the Number of Ideal Arrays</a></h3><p>You are given two integers <code>n</code> and <code>maxValue</code>, which are used to describe an <strong>ideal</strong> array.</p>
<p>A <strong>0-indexed</strong> integer array <code>arr</code> of length <code>n</code> is considered <strong>ideal</strong> if the following conditions hold:</p>
<ul>
<li>Every <code>arr[i]</code> is a value from <code>1</code> to <code>maxValue</code>, for <code>0 &lt;= i &lt; n</code>.</li>
<li>Every <code>arr[i]</code> is divisible by <code>arr[i - 1]</code>, for <code>0 &lt; i &lt; n</code>.</li>
</ul>
<p>Return <em>the number of <strong>distinct</strong> ideal arrays of length</em> <code>n</code>. Since the answer may be very large, return it modulo <code>109 + 7</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Input: n &#x3D; 2, maxValue &#x3D; 5</span><br><span class="line">Output: 10</span><br><span class="line">Explanation: The following are the possible ideal arrays:</span><br><span class="line">- Arrays starting with the value 1 (5 arrays): [1,1], [1,2], [1,3], [1,4], [1,5]</span><br><span class="line">- Arrays starting with the value 2 (2 arrays): [2,2], [2,4]</span><br><span class="line">- Arrays starting with the value 3 (1 array): [3,3]</span><br><span class="line">- Arrays starting with the value 4 (1 array): [4,4]</span><br><span class="line">- Arrays starting with the value 5 (1 array): [5,5]</span><br><span class="line">There are a total of 5 + 2 + 1 + 1 + 1 &#x3D; 10 distinct ideal arrays.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Input: n &#x3D; 5, maxValue &#x3D; 3</span><br><span class="line">Output: 11</span><br><span class="line">Explanation: The following are the possible ideal arrays:</span><br><span class="line">- Arrays starting with the value 1 (9 arrays): </span><br><span class="line">   - With no other distinct values (1 array): [1,1,1,1,1] </span><br><span class="line">   - With 2nd distinct value 2 (4 arrays): [1,1,1,1,2], [1,1,1,2,2], [1,1,2,2,2], [1,2,2,2,2]</span><br><span class="line">   - With 2nd distinct value 3 (4 arrays): [1,1,1,1,3], [1,1,1,3,3], [1,1,3,3,3], [1,3,3,3,3]</span><br><span class="line">- Arrays starting with the value 2 (1 array): [2,2,2,2,2]</span><br><span class="line">- Arrays starting with the value 3 (1 array): [3,3,3,3,3]</span><br><span class="line">There are a total of 9 + 1 + 1 &#x3D; 11 distinct ideal arrays.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= n &lt;= 104</code></li>
<li><code>1 &lt;= maxValue &lt;= 104</code></li>
</ul>
<p>Hard，小于maxValue的所有数分解质因数，再进行计算</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line">    <span class="keyword">const</span> <span class="keyword">int</span> MOD = <span class="number">1000000007</span>;</span><br><span class="line">    <span class="built_in">vector</span>&lt;<span class="keyword">long</span> <span class="keyword">long</span>&gt; inv;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// O(b) 求组合数</span></span><br><span class="line">    <span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">C</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (b &gt; a) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> ret = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= b; i++) ret = (ret * (a - i + <span class="number">1</span>) % MOD * inv[i]) % MOD;</span><br><span class="line">        <span class="keyword">return</span> ret;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">idealArrays</span><span class="params">(<span class="keyword">int</span> n, <span class="keyword">int</span> K)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// nlnn 进行质因数分解，f[i] 的元素表示 i 的每种质因数的指数</span></span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; f(K + <span class="number">1</span>);</span><br><span class="line">        <span class="keyword">int</span> mx = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">2</span>; i &lt;= K; i++) <span class="keyword">if</span> (f[i].empty()) <span class="keyword">for</span> (<span class="keyword">int</span> j = i; j &lt;= K; j += i) &#123;</span><br><span class="line">            <span class="keyword">int</span> x = j, y = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">for</span> (; x % i == <span class="number">0</span>; x /= i) y++;</span><br><span class="line">            f[j].push_back(y);</span><br><span class="line">            mx = max(mx, y);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 线性求逆元</span></span><br><span class="line">        inv.resize(mx + <span class="number">5</span>);</span><br><span class="line">        inv[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">2</span>; i &lt;= mx; i++) inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 统计答案</span></span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> ans = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= K; i++) &#123;</span><br><span class="line">            <span class="keyword">long</span> <span class="keyword">long</span> t = <span class="number">1</span>;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> x : f[i]) t = (t * C(n + x - <span class="number">1</span>, x)) % MOD;</span><br><span class="line">            ans = (ans + t) % MOD;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-302"><a href="#Weekly-Contest-302" class="headerlink" title="Weekly Contest 302"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-302/">Weekly Contest 302</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220718090335.png" alt="image-20220718090303244"></p>
<h3 id="2341-Maximum-Number-of-Pairs-in-Array"><a href="#2341-Maximum-Number-of-Pairs-in-Array" class="headerlink" title="2341. Maximum Number of Pairs in Array"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/maximum-number-of-pairs-in-array/">2341. Maximum Number of Pairs in Array</a></h3><p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation, you may do the following:</p>
<ul>
<li>Choose <strong>two</strong> integers in <code>nums</code> that are <strong>equal</strong>.</li>
<li>Remove both integers from <code>nums</code>, forming a <strong>pair</strong>.</li>
</ul>
<p>The operation is done on <code>nums</code> as many times as possible.</p>
<p>Return <em>a <strong>0-indexed</strong> integer array</em> <code>answer</code> <em>of size</em> <code>2</code> <em>where</em> <code>answer[0]</code> <em>is the number of pairs that are formed and</em> <code>answer[1]</code> <em>is the number of leftover integers in</em> <code>nums</code> <em>after doing the operation as many times as possible</em>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [1,3,2,1,3,2,2]</span><br><span class="line">Output: [3,1]</span><br><span class="line">Explanation:</span><br><span class="line">Form a pair with nums[0] and nums[3] and remove them from nums. Now, nums &#x3D; [3,2,3,2,2].</span><br><span class="line">Form a pair with nums[0] and nums[2] and remove them from nums. Now, nums &#x3D; [2,2,2].</span><br><span class="line">Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums &#x3D; [2].</span><br><span class="line">No more pairs can be formed. A total of 3 pairs have been formed, and there is 1 number leftover in nums.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [1,1]</span><br><span class="line">Output: [1,0]</span><br><span class="line">Explanation: Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums &#x3D; [].</span><br><span class="line">No more pairs can be formed. A total of 1 pair has been formed, and there are 0 numbers leftover in nums.</span><br></pre></td></tr></table></figure>

<p><strong>Example 3:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [0]</span><br><span class="line">Output: [0,1]</span><br><span class="line">Explanation: No pairs can be formed, and there is 1 number leftover in nums.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
<li><code>0 &lt;= nums[i] &lt;= 100</code></li>
</ul>
<p>Easy，计算每个数出现次数是奇数还是偶数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">numberOfPairs</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">countNums</span><span class="params">(<span class="number">101</span>, <span class="number">0</span>)</span></span>;</span><br><span class="line">        <span class="keyword">int</span> odd = <span class="number">0</span>, even = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> num: nums) &#123;</span><br><span class="line">            countNums[num]++;</span><br><span class="line">            <span class="keyword">if</span> (countNums[num] % <span class="number">2</span> == <span class="number">1</span>) &#123;</span><br><span class="line">                odd++;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                even++;</span><br><span class="line">                odd--;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> &#123;even, odd&#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2342-Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits"><a href="#2342-Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits" class="headerlink" title="2342. Max Sum of a Pair With Equal Sum of Digits"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/max-sum-of-a-pair-with-equal-sum-of-digits/">2342. Max Sum of a Pair With Equal Sum of Digits</a></h3><p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of <strong>positive</strong> integers. You can choose two indices <code>i</code> and <code>j</code>, such that <code>i != j</code>, and the sum of digits of the number <code>nums[i]</code> is equal to that of <code>nums[j]</code>.</p>
<p>Return <em>the <strong>maximum</strong> value of</em> <code>nums[i] + nums[j]</code> <em>that you can obtain over all possible indices</em> <code>i</code> <em>and</em> <code>j</code> <em>that satisfy the conditions.</em></p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [18,43,36,13,7]</span><br><span class="line">Output: 54</span><br><span class="line">Explanation: The pairs (i, j) that satisfy the conditions are:</span><br><span class="line">- (0, 2), both numbers have a sum of digits equal to 9, and their sum is 18 + 36 &#x3D; 54.</span><br><span class="line">- (1, 4), both numbers have a sum of digits equal to 7, and their sum is 43 + 7 &#x3D; 50.</span><br><span class="line">So the maximum sum that we can obtain is 54.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [10,12,19,14]</span><br><span class="line">Output: -1</span><br><span class="line">Explanation: There are no two numbers that satisfy the conditions, so we return -1.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 105</code></li>
<li><code>1 &lt;= nums[i] &lt;= 109</code></li>
</ul>
<p>Medium，计算位数相同的数，保留最大的两个</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">maximumSum</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">-1</span>;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; digitSumWithMAxTwo (<span class="number">82</span>, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; (<span class="number">3</span>, <span class="number">0</span>));</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> num: nums) &#123;</span><br><span class="line">            <span class="keyword">int</span> digitSum = countDigitSum(num);</span><br><span class="line">            digitSumWithMAxTwo[digitSum][<span class="number">0</span>]++;</span><br><span class="line">            <span class="keyword">if</span> (digitSumWithMAxTwo[digitSum][<span class="number">1</span>] == <span class="number">0</span>) &#123;</span><br><span class="line">                digitSumWithMAxTwo[digitSum][<span class="number">1</span>] = num;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (digitSumWithMAxTwo[digitSum][<span class="number">2</span>] == <span class="number">0</span>) &#123;</span><br><span class="line">                digitSumWithMAxTwo[digitSum][<span class="number">2</span>] = num;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (num &gt; digitSumWithMAxTwo[digitSum][<span class="number">1</span>]) &#123;</span><br><span class="line">                digitSumWithMAxTwo[digitSum][<span class="number">2</span>] = digitSumWithMAxTwo[digitSum][<span class="number">1</span>];</span><br><span class="line">                digitSumWithMAxTwo[digitSum][<span class="number">1</span>] = num;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (num &gt; digitSumWithMAxTwo[digitSum][<span class="number">2</span>]) &#123;</span><br><span class="line">                digitSumWithMAxTwo[digitSum][<span class="number">2</span>] = num;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// cout&lt;&lt;digitSum&lt;&lt;&quot;,&quot;&lt;&lt;digitSumWithMAxTwo[digitSum][0]&lt;&lt;&quot;,&quot;&lt;&lt;digitSumWithMAxTwo[digitSum][1]&lt;&lt;&quot;,&quot;&lt;&lt;digitSumWithMAxTwo[digitSum][2]&lt;&lt;endl;</span></span><br><span class="line">            <span class="keyword">if</span> (digitSumWithMAxTwo[digitSum][<span class="number">0</span>] &gt; <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (digitSumWithMAxTwo[digitSum][<span class="number">1</span>] &lt; digitSumWithMAxTwo[digitSum][<span class="number">2</span>]) &#123;</span><br><span class="line">                    <span class="keyword">int</span> temp = digitSumWithMAxTwo[digitSum][<span class="number">1</span>];</span><br><span class="line">                    digitSumWithMAxTwo[digitSum][<span class="number">1</span>] = digitSumWithMAxTwo[digitSum][<span class="number">2</span>];</span><br><span class="line">                    digitSumWithMAxTwo[digitSum][<span class="number">2</span>] = temp;</span><br><span class="line">                &#125;</span><br><span class="line">                res = max(res, digitSumWithMAxTwo[digitSum][<span class="number">1</span>] + digitSumWithMAxTwo[digitSum][<span class="number">2</span>]);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">countDigitSum</span><span class="params">(<span class="keyword">int</span> num)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span> (num != <span class="number">0</span>) &#123;</span><br><span class="line">            res += num % <span class="number">10</span>;</span><br><span class="line">            num = num / <span class="number">10</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2343-Query-Kth-Smallest-Trimmed-Number"><a href="#2343-Query-Kth-Smallest-Trimmed-Number" class="headerlink" title="2343. Query Kth Smallest Trimmed Number"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/query-kth-smallest-trimmed-number/">2343. Query Kth Smallest Trimmed Number</a></h3><p>You are given a <strong>0-indexed</strong> array of strings <code>nums</code>, where each string is of <strong>equal length</strong> and consists of only digits.</p>
<p>You are also given a <strong>0-indexed</strong> 2D integer array <code>queries</code> where <code>queries[i] = [ki, trimi]</code>. For each <code>queries[i]</code>, you need to:</p>
<ul>
<li><strong>Trim</strong> each number in <code>nums</code> to its <strong>rightmost</strong> <code>trimi</code> digits.</li>
<li>Determine the <strong>index</strong> of the <code>kith</code> smallest trimmed number in <code>nums</code>. If two trimmed numbers are equal, the number with the <strong>lower</strong> index is considered to be smaller.</li>
<li>Reset each number in <code>nums</code> to its original length.</li>
</ul>
<p>Return <em>an array</em> <code>answer</code> <em>of the same length as</em> <code>queries</code>, <em>where</em> <code>answer[i]</code> <em>is the answer to the</em> <code>ith</code> <em>query.</em></p>
<p><strong>Note</strong>:</p>
<ul>
<li>To trim to the rightmost <code>x</code> digits means to keep removing the leftmost digit, until only <code>x</code> digits remain.</li>
<li>Strings in <code>nums</code> may contain leading zeros.</li>
</ul>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [&quot;102&quot;,&quot;473&quot;,&quot;251&quot;,&quot;814&quot;], queries &#x3D; [[1,1],[2,3],[4,2],[1,2]]</span><br><span class="line">Output: [2,2,1,0]</span><br><span class="line">Explanation:</span><br><span class="line">1. After trimming to the last digit, nums &#x3D; [&quot;2&quot;,&quot;3&quot;,&quot;1&quot;,&quot;4&quot;]. The smallest number is 1 at index 2.</span><br><span class="line">2. Trimmed to the last 3 digits, nums is unchanged. The 2nd smallest number is 251 at index 2.</span><br><span class="line">3. Trimmed to the last 2 digits, nums &#x3D; [&quot;02&quot;,&quot;73&quot;,&quot;51&quot;,&quot;14&quot;]. The 4th smallest number is 73.</span><br><span class="line">4. Trimmed to the last 2 digits, the smallest number is 2 at index 0.</span><br><span class="line">   Note that the trimmed number &quot;02&quot; is evaluated as 2.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [&quot;24&quot;,&quot;37&quot;,&quot;96&quot;,&quot;04&quot;], queries &#x3D; [[2,1],[2,2]]</span><br><span class="line">Output: [3,0]</span><br><span class="line">Explanation:</span><br><span class="line">1. Trimmed to the last digit, nums &#x3D; [&quot;4&quot;,&quot;7&quot;,&quot;6&quot;,&quot;4&quot;]. The 2nd smallest number is 4 at index 3.</span><br><span class="line">   There are two occurrences of 4, but the one at index 0 is considered smaller than the one at index 3.</span><br><span class="line">2. Trimmed to the last 2 digits, nums is unchanged. The 2nd smallest number is 24.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
<li><code>1 &lt;= nums[i].length &lt;= 100</code></li>
<li><code>nums[i]</code> consists of only digits.</li>
<li>All <code>nums[i].length</code> are <strong>equal</strong>.</li>
<li><code>1 &lt;= queries.length &lt;= 100</code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>1 &lt;= ki &lt;= nums.length</code></li>
<li><code>1 &lt;= trimi &lt;= nums[i].length</code></li>
</ul>
<p>Medium，保存后几位数的大小排序并保存原索引</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">smallestTrimmedNumbers</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt;&amp; nums, <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; queries)</span> </span>&#123;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">bool</span>&gt; <span class="title">isSearched</span><span class="params">(<span class="number">100</span>, <span class="literal">false</span>)</span></span>;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="built_in">pair</span>&lt;<span class="built_in">string</span>, <span class="keyword">int</span>&gt;&gt;&gt; smallestTrimmedInorder(<span class="number">100</span>);</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; res;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">auto</span> &amp;query: queries) &#123;</span><br><span class="line">            <span class="keyword">if</span> (isSearched[query[<span class="number">1</span>] - <span class="number">1</span>]) &#123;</span><br><span class="line">                res.push_back(smallestTrimmedInorder[query[<span class="number">1</span>] - <span class="number">1</span>][query[<span class="number">0</span>] - <span class="number">1</span>].second);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                isSearched[query[<span class="number">1</span>] - <span class="number">1</span>] = <span class="literal">true</span>;</span><br><span class="line">                <span class="built_in">vector</span>&lt;<span class="built_in">pair</span>&lt;<span class="built_in">string</span>, <span class="keyword">int</span>&gt;&gt; arr;</span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; nums.size(); j++) &#123;</span><br><span class="line">                    <span class="built_in">string</span> sub = nums[j].substr(nums[j].size() - query[<span class="number">1</span>]);</span><br><span class="line">                    arr.push_back(<span class="built_in">make_pair</span>(sub, j));</span><br><span class="line">                &#125;</span><br><span class="line">                sort(arr.begin(), arr.end());</span><br><span class="line">                smallestTrimmedInorder[query[<span class="number">1</span>] - <span class="number">1</span>] = arr;</span><br><span class="line">                res.push_back(smallestTrimmedInorder[query[<span class="number">1</span>] - <span class="number">1</span>][query[<span class="number">0</span>] - <span class="number">1</span>].second);</span><br><span class="line">            &#125;</span><br><span class="line">            </span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2344-Minimum-Deletions-to-Make-Array-Divisible"><a href="#2344-Minimum-Deletions-to-Make-Array-Divisible" class="headerlink" title="2344. Minimum Deletions to Make Array Divisible"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/minimum-deletions-to-make-array-divisible/">2344. Minimum Deletions to Make Array Divisible</a></h3><p>You are given two positive integer arrays <code>nums</code> and <code>numsDivide</code>. You can delete any number of elements from <code>nums</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions such that the <strong>smallest</strong> element in</em> <code>nums</code> *<strong>divides</strong> all the elements of* <code>numsDivide</code>. If this is not possible, return <code>-1</code>.</p>
<p>Note that an integer <code>x</code> divides <code>y</code> if <code>y % x == 0</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [2,3,2,4,3], numsDivide &#x3D; [9,6,9,3,15]</span><br><span class="line">Output: 2</span><br><span class="line">Explanation: </span><br><span class="line">The smallest element in [2,3,2,4,3] is 2, which does not divide all the elements of numsDivide.</span><br><span class="line">We use 2 deletions to delete the elements in nums that are equal to 2 which makes nums &#x3D; [3,4,3].</span><br><span class="line">The smallest element in [3,4,3] is 3, which divides all the elements of numsDivide.</span><br><span class="line">It can be shown that 2 is the minimum number of deletions needed.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [4,3,6], numsDivide &#x3D; [8,2,6,10]</span><br><span class="line">Output: -1</span><br><span class="line">Explanation: </span><br><span class="line">We want the smallest element in nums to divide all the elements of numsDivide.</span><br><span class="line">There is no way to delete elements from nums to allow this.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length, numsDivide.length &lt;= 105</code></li>
<li><code>1 &lt;= nums[i], numsDivide[i] &lt;= 109</code></li>
</ul>
<p>Hard，求最大公约数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">minOperations</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; numsDivide)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> gcdNum = gcd(numsDivide);</span><br><span class="line">        sort(nums.begin(), nums.end());</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; nums.size(); i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (nums[i] &gt; gcdNum) <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">            <span class="keyword">if</span> (gcdNum % nums[i] == <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> i;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">gcd</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; nums)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> res = nums[<span class="number">0</span>];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; nums.size(); i++) &#123;</span><br><span class="line">            res = gcdTwoNum(res, nums[i]);</span><br><span class="line">            <span class="keyword">if</span> (res == <span class="number">1</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">gcdTwoNum</span><span class="params">(<span class="keyword">int</span> num1, <span class="keyword">int</span> num2)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (num1 &gt; num2) &#123;</span><br><span class="line">            <span class="keyword">return</span> gcdTwoNum(num2, num1);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">while</span> (num1 != <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">int</span> q = num2 % num1;</span><br><span class="line">            <span class="keyword">if</span> (q == <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> num1;</span><br><span class="line">            &#125;</span><br><span class="line">            num2 = num1;</span><br><span class="line">            num1 = q;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> num1;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-303"><a href="#Weekly-Contest-303" class="headerlink" title="Weekly Contest 303"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-303/">Weekly Contest 303</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220724222818.png" alt="image-20220724222811908"></p>
<h3 id="2351-First-Letter-to-Appear-Twice"><a href="#2351-First-Letter-to-Appear-Twice" class="headerlink" title="2351. First Letter to Appear Twice"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/first-letter-to-appear-twice/">2351. First Letter to Appear Twice</a></h3><p>Given a string <code>s</code> consisting of lowercase English letters, return <em>the first letter to appear <strong>twice</strong></em>.</p>
<p><strong>Note</strong>:</p>
<ul>
<li>A letter <code>a</code> appears twice before another letter <code>b</code> if the <strong>second</strong> occurrence of <code>a</code> is before the <strong>second</strong> occurrence of <code>b</code>.</li>
<li><code>s</code> will contain at least one letter that appears twice.</li>
</ul>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;abccbaacz&quot;</span><br><span class="line">Output: &quot;c&quot;</span><br><span class="line">Explanation:</span><br><span class="line">The letter &#39;a&#39; appears on the indexes 0, 5 and 6.</span><br><span class="line">The letter &#39;b&#39; appears on the indexes 1 and 4.</span><br><span class="line">The letter &#39;c&#39; appears on the indexes 2, 3 and 7.</span><br><span class="line">The letter &#39;z&#39; appears on the index 8.</span><br><span class="line">The letter &#39;c&#39; is the first letter to appear twice, because out of all the letters the index of its second occurrence is the smallest.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;abcdd&quot;</span><br><span class="line">Output: &quot;d&quot;</span><br><span class="line">Explanation:</span><br><span class="line">The only letter that appears twice is &#39;d&#39; so we return &#39;d&#39;.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= s.length &lt;= 100</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
<li><code>s</code> has at least one repeated letter.</li>
</ul>
<p>Easy，哈希</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">char</span> <span class="title">repeatedCharacter</span><span class="params">(<span class="built_in">string</span> s)</span> </span>&#123;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">bool</span>&gt; <span class="title">isVisited</span><span class="params">(<span class="number">26</span>, <span class="literal">false</span>)</span></span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">char</span> c: s) &#123;</span><br><span class="line">            <span class="keyword">if</span> (isVisited[c - <span class="string">&#x27;a&#x27;</span>] == <span class="literal">true</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> c;</span><br><span class="line">            &#125;</span><br><span class="line">            isVisited[c - <span class="string">&#x27;a&#x27;</span>] = <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2352-Equal-Row-and-Column-Pairs"><a href="#2352-Equal-Row-and-Column-Pairs" class="headerlink" title="2352. Equal Row and Column Pairs"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/equal-row-and-column-pairs/">2352. Equal Row and Column Pairs</a></h3><p>Given a <strong>0-indexed</strong> <code>n x n</code> integer matrix <code>grid</code>, <em>return the number of pairs</em> <code>(Ri, Cj)</code> <em>such that row</em> <code>Ri</code> <em>and column</em> <code>Cj</code> <em>are equal</em>.</p>
<p>A row and column pair is considered equal if they contain the same elements in the same order (i.e. an equal array).</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220724224120.jpeg" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: grid &#x3D; [[3,2,1],[1,7,6],[2,7,7]]</span><br><span class="line">Output: 1</span><br><span class="line">Explanation: There is 1 equal row and column pair:</span><br><span class="line">- (Row 2, Column 1): [2,7,7]</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220724224146.jpeg" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: grid &#x3D; [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]</span><br><span class="line">Output: 3</span><br><span class="line">Explanation: There are 3 equal row and column pairs:</span><br><span class="line">- (Row 0, Column 0): [3,1,2,2]</span><br><span class="line">- (Row 2, Column 2): [2,4,2,2]</span><br><span class="line">- (Row 3, Column 2): [2,4,2,2]</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == grid.length == grid[i].length</code></li>
<li><code>1 &lt;= n &lt;= 200</code></li>
<li><code>1 &lt;= grid[i][j] &lt;= 105</code></li>
</ul>
<p>Medium，拼接字符串+哈希</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">equalPairs</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; grid)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> n = grid.size();</span><br><span class="line">        <span class="built_in">unordered_map</span>&lt;<span class="built_in">string</span>, <span class="keyword">int</span>&gt; dic;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="built_in">string</span> row;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; n; j++) &#123;</span><br><span class="line">                row += to_string (grid[i][j]);</span><br><span class="line">                row += <span class="string">&#x27;,&#x27;</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            dic[row]++;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="built_in">string</span> col;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; n; j++) &#123;</span><br><span class="line">                col += to_string (grid[j][i]);</span><br><span class="line">                col += <span class="string">&#x27;,&#x27;</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (dic.count(col)) res += dic[col];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2353-Design-a-Food-Rating-System"><a href="#Failed-2353-Design-a-Food-Rating-System" class="headerlink" title="Failed: 2353. Design a Food Rating System"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/design-a-food-rating-system/">2353. Design a Food Rating System</a></h3><p>Design a food rating system that can do the following:</p>
<ul>
<li><strong>Modify</strong> the rating of a food item listed in the system.</li>
<li>Return the highest-rated food item for a type of cuisine in the system.</li>
</ul>
<p>Implement the <code>FoodRatings</code> class:</p>
<ul>
<li><p><code>FoodRatings(String[] foods, String[] cuisines, int[] ratings)</code></p>
<p>Initializes the system. The food items are described by<code>foods</code>,<code>cuisines</code>and<code>ratings</code>, all of which have a length of<code>n</code>.</p>
<ul>
<li><code>foods[i]</code> is the name of the <code>ith</code> food,</li>
<li><code>cuisines[i]</code> is the type of cuisine of the <code>ith</code> food, and</li>
<li><code>ratings[i]</code> is the initial rating of the <code>ith</code> food.</li>
</ul>
</li>
<li><p><code>void changeRating(String food, int newRating)</code> Changes the rating of the food item with the name <code>food</code>.</p>
</li>
<li><p><code>String highestRated(String cuisine)</code> Returns the name of the food item that has the highest rating for the given type of <code>cuisine</code>. If there is a tie, return the item with the <strong>lexicographically smaller</strong> name.</p>
</li>
</ul>
<p>Note that a string <code>x</code> is lexicographically smaller than string <code>y</code> if <code>x</code> comes before <code>y</code> in dictionary order, that is, either <code>x</code> is a prefix of <code>y</code>, or if <code>i</code> is the first position such that <code>x[i] != y[i]</code>, then <code>x[i]</code> comes before <code>y[i]</code> in alphabetic order.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">Input</span><br><span class="line">[&quot;FoodRatings&quot;, &quot;highestRated&quot;, &quot;highestRated&quot;, &quot;changeRating&quot;, &quot;highestRated&quot;, &quot;changeRating&quot;, &quot;highestRated&quot;]</span><br><span class="line">[[[&quot;kimchi&quot;, &quot;miso&quot;, &quot;sushi&quot;, &quot;moussaka&quot;, &quot;ramen&quot;, &quot;bulgogi&quot;], [&quot;korean&quot;, &quot;japanese&quot;, &quot;japanese&quot;, &quot;greek&quot;, &quot;japanese&quot;, &quot;korean&quot;], [9, 12, 8, 15, 14, 7]], [&quot;korean&quot;], [&quot;japanese&quot;], [&quot;sushi&quot;, 16], [&quot;japanese&quot;], [&quot;ramen&quot;, 16], [&quot;japanese&quot;]]</span><br><span class="line">Output</span><br><span class="line">[null, &quot;kimchi&quot;, &quot;ramen&quot;, null, &quot;sushi&quot;, null, &quot;ramen&quot;]</span><br><span class="line"></span><br><span class="line">Explanation</span><br><span class="line">FoodRatings foodRatings &#x3D; new FoodRatings([&quot;kimchi&quot;, &quot;miso&quot;, &quot;sushi&quot;, &quot;moussaka&quot;, &quot;ramen&quot;, &quot;bulgogi&quot;], [&quot;korean&quot;, &quot;japanese&quot;, &quot;japanese&quot;, &quot;greek&quot;, &quot;japanese&quot;, &quot;korean&quot;], [9, 12, 8, 15, 14, 7]);</span><br><span class="line">foodRatings.highestRated(&quot;korean&quot;); &#x2F;&#x2F; return &quot;kimchi&quot;</span><br><span class="line">                                    &#x2F;&#x2F; &quot;kimchi&quot; is the highest rated korean food with a rating of 9.</span><br><span class="line">foodRatings.highestRated(&quot;japanese&quot;); &#x2F;&#x2F; return &quot;ramen&quot;</span><br><span class="line">                                      &#x2F;&#x2F; &quot;ramen&quot; is the highest rated japanese food with a rating of 14.</span><br><span class="line">foodRatings.changeRating(&quot;sushi&quot;, 16); &#x2F;&#x2F; &quot;sushi&quot; now has a rating of 16.</span><br><span class="line">foodRatings.highestRated(&quot;japanese&quot;); &#x2F;&#x2F; return &quot;sushi&quot;</span><br><span class="line">                                      &#x2F;&#x2F; &quot;sushi&quot; is the highest rated japanese food with a rating of 16.</span><br><span class="line">foodRatings.changeRating(&quot;ramen&quot;, 16); &#x2F;&#x2F; &quot;ramen&quot; now has a rating of 16.</span><br><span class="line">foodRatings.highestRated(&quot;japanese&quot;); &#x2F;&#x2F; return &quot;ramen&quot;</span><br><span class="line">                                      &#x2F;&#x2F; Both &quot;sushi&quot; and &quot;ramen&quot; have a rating of 16.</span><br><span class="line">                                      &#x2F;&#x2F; However, &quot;ramen&quot; is lexicographically smaller than &quot;sushi&quot;.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 2 * 104</code></li>
<li><code>n == foods.length == cuisines.length == ratings.length</code></li>
<li><code>1 &lt;= foods[i].length, cuisines[i].length &lt;= 10</code></li>
<li><code>foods[i]</code>, <code>cuisines[i]</code> consist of lowercase English letters.</li>
<li><code>1 &lt;= ratings[i] &lt;= 108</code></li>
<li>All the strings in <code>foods</code> are <strong>distinct</strong>.</li>
<li><code>food</code> will be the name of a food item in the system across all calls to <code>changeRating</code>.</li>
<li><code>cuisine</code> will be a type of cuisine of <strong>at least one</strong> food item in the system across all calls to <code>highestRated</code>.</li>
<li>At most <code>2 * 104</code> calls <strong>in total</strong> will be made to <code>changeRating</code> and <code>highestRated</code>.</li>
</ul>
<p>Medium，STL接口不熟悉，哎</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">FoodRatings</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Cmp</span> &#123;</span></span><br><span class="line">        <span class="function"><span class="keyword">bool</span> <span class="title">operator</span> <span class="params">()</span> <span class="params">(<span class="keyword">const</span> <span class="built_in">pair</span>&lt;<span class="keyword">int</span>, <span class="built_in">string</span>&gt;&amp;a, <span class="keyword">const</span> <span class="built_in">pair</span>&lt;<span class="keyword">int</span>, <span class="built_in">string</span>&gt;&amp;b)</span> <span class="keyword">const</span> </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (a.first == b.first)</span><br><span class="line">                <span class="keyword">return</span> a.second &lt; b.second;</span><br><span class="line">            <span class="keyword">return</span> a.first &gt; b.first;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;;</span><br><span class="line">	<span class="built_in">unordered_map</span>&lt;<span class="built_in">string</span>, <span class="built_in">pair</span>&lt;<span class="keyword">int</span>, <span class="built_in">string</span>&gt;&gt; foodDic;</span><br><span class="line">	<span class="built_in">unordered_map</span>&lt;<span class="built_in">string</span>, <span class="built_in">set</span>&lt;<span class="built_in">pair</span>&lt;<span class="keyword">int</span>, <span class="built_in">string</span>&gt;, Cmp&gt;&gt; cuisineDic;</span><br><span class="line">	FoodRatings(<span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt;&amp; foods, <span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt;&amp; cuisines, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; ratings) &#123;</span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; foods.size(); i++) &#123;</span><br><span class="line">			foodDic[foods[i]]= <span class="built_in">make_pair</span>(ratings[i], cuisines[i]);</span><br><span class="line">			cuisineDic[cuisines[i]].insert(<span class="built_in">make_pair</span>(ratings[i], foods[i]));</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">changeRating</span><span class="params">(<span class="built_in">string</span> food, <span class="keyword">int</span> newRating)</span> </span>&#123;</span><br><span class="line">		<span class="built_in">string</span> cuisine = foodDic[food].second;</span><br><span class="line">		<span class="keyword">int</span> rate = foodDic[food].first;</span><br><span class="line">		cuisineDic[cuisine].erase(<span class="built_in">make_pair</span>(rate, food));</span><br><span class="line">		cuisineDic[cuisine].insert(<span class="built_in">make_pair</span>(newRating, food));</span><br><span class="line">        foodDic[food]= <span class="built_in">make_pair</span>(newRating, cuisine);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="built_in">string</span> <span class="title">highestRated</span><span class="params">(<span class="built_in">string</span> cuisine)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> cuisineDic[cuisine].begin()-&gt;second;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Your FoodRatings object will be instantiated and called as such:</span></span><br><span class="line"><span class="comment"> * FoodRatings* obj = new FoodRatings(foods, cuisines, ratings);</span></span><br><span class="line"><span class="comment"> * obj-&gt;changeRating(food,newRating);</span></span><br><span class="line"><span class="comment"> * string param_2 = obj-&gt;highestRated(cuisine);</span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>



<h3 id="Failed-2354-Number-of-Excellent-Pairs"><a href="#Failed-2354-Number-of-Excellent-Pairs" class="headerlink" title="Failed: 2354. Number of Excellent Pairs"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/number-of-excellent-pairs/">2354. Number of Excellent Pairs</a></h3><p>You are given a <strong>0-indexed</strong> positive integer array <code>nums</code> and a positive integer <code>k</code>.</p>
<p>A pair of numbers <code>(num1, num2)</code> is called <strong>excellent</strong> if the following conditions are satisfied:</p>
<ul>
<li><strong>Both</strong> the numbers <code>num1</code> and <code>num2</code> exist in the array <code>nums</code>.</li>
<li>The sum of the number of set bits in <code>num1 OR num2</code> and <code>num1 AND num2</code> is greater than or equal to <code>k</code>, where <code>OR</code> is the bitwise <strong>OR</strong> operation and <code>AND</code> is the bitwise <strong>AND</strong> operation.</li>
</ul>
<p>Return <em>the number of <strong>distinct</strong> excellent pairs</em>.</p>
<p>Two pairs <code>(a, b)</code> and <code>(c, d)</code> are considered distinct if either <code>a != c</code> or <code>b != d</code>. For example, <code>(1, 2)</code> and <code>(2, 1)</code> are distinct.</p>
<p><strong>Note</strong> that a pair <code>(num1, num2)</code> such that <code>num1 == num2</code> can also be excellent if you have at least <strong>one</strong> occurrence of <code>num1</code> in the array.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [1,2,3,1], k &#x3D; 3</span><br><span class="line">Output: 5</span><br><span class="line">Explanation: The excellent pairs are the following:</span><br><span class="line">- (3, 3). (3 AND 3) and (3 OR 3) are both equal to (11) in binary. The total number of set bits is 2 + 2 &#x3D; 4, which is greater than or equal to k &#x3D; 3.</span><br><span class="line">- (2, 3) and (3, 2). (2 AND 3) is equal to (10) in binary, and (2 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 &#x3D; 3.</span><br><span class="line">- (1, 3) and (3, 1). (1 AND 3) is equal to (01) in binary, and (1 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 &#x3D; 3.</span><br><span class="line">So the number of excellent pairs is 5.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [5,1,1], k &#x3D; 10</span><br><span class="line">Output: 0</span><br><span class="line">Explanation: There are no excellent pairs for this array.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 105</code></li>
<li><code>1 &lt;= nums[i] &lt;= 109</code></li>
<li><code>1 &lt;= k &lt;= 60</code></li>
</ul>
<p>Hard，其实就是求每个数有多少1</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">countExcellentPairs</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> res = <span class="number">0</span>;</span><br><span class="line">        <span class="built_in">unordered_map</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; bitCounts;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> num: <span class="built_in">unordered_set</span>&lt;<span class="keyword">int</span>&gt;(nums.begin(), nums.end())) &#123;</span><br><span class="line">            bitCounts[__builtin_popcount(num)]++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">auto</span> &amp;[cx, ccx] : bitCounts) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">auto</span> &amp;[cy, ccy] : bitCounts) &#123;</span><br><span class="line">                <span class="keyword">if</span> (cx + cy &gt;= k) &#123;</span><br><span class="line">                    res += ccx * ccy;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-304"><a href="#Weekly-Contest-304" class="headerlink" title="Weekly Contest 304"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-304/">Weekly Contest 304</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220801091228.png" alt="image-20220801091222647"></p>
<h3 id="2357-Make-Array-Zero-by-Subtracting-Equal-Amounts"><a href="#2357-Make-Array-Zero-by-Subtracting-Equal-Amounts" class="headerlink" title="2357. Make Array Zero by Subtracting Equal Amounts"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts/">2357. Make Array Zero by Subtracting Equal Amounts</a></h3><p>You are given a non-negative integer array <code>nums</code>. In one operation, you must:</p>
<ul>
<li>Choose a positive integer <code>x</code> such that <code>x</code> is less than or equal to the <strong>smallest non-zero</strong> element in <code>nums</code>.</li>
<li>Subtract <code>x</code> from every <strong>positive</strong> element in <code>nums</code>.</li>
</ul>
<p>Return <em>the <strong>minimum</strong> number of operations to make every element in</em> <code>nums</code> <em>equal to</em> <code>0</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [1,5,0,3,5]</span><br><span class="line">Output: 3</span><br><span class="line">Explanation:</span><br><span class="line">In the first operation, choose x &#x3D; 1. Now, nums &#x3D; [0,4,0,2,4].</span><br><span class="line">In the second operation, choose x &#x3D; 2. Now, nums &#x3D; [0,2,0,0,2].</span><br><span class="line">In the third operation, choose x &#x3D; 2. Now, nums &#x3D; [0,0,0,0,0].</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [0]</span><br><span class="line">Output: 0</span><br><span class="line">Explanation: Each element in nums is already 0 so no operations are needed.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
<li><code>0 &lt;= nums[i] &lt;= 100</code></li>
</ul>
<p>Easy，保存每一轮之后的数据</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">minimumOperations</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">set</span>&lt;<span class="keyword">int</span>&gt; pos;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> num: nums) &#123;</span><br><span class="line">            <span class="keyword">if</span> (num &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                pos.insert(num);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span> (pos.size() &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">auto</span> it = pos.begin();</span><br><span class="line">            <span class="keyword">int</span> min_num = *it;</span><br><span class="line">            <span class="built_in">set</span>&lt;<span class="keyword">int</span>&gt; temp;</span><br><span class="line">            <span class="keyword">for</span> (; it != pos.end(); it++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (*it &gt; min_num) &#123;</span><br><span class="line">                    temp.insert(*it - min_num);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            pos = temp;</span><br><span class="line">            ans++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2358-Maximum-Number-of-Groups-Entering-a-Competition"><a href="#Failed-2358-Maximum-Number-of-Groups-Entering-a-Competition" class="headerlink" title="Failed: 2358. Maximum Number of Groups Entering a Competition"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/maximum-number-of-groups-entering-a-competition/">2358. Maximum Number of Groups Entering a Competition</a></h3><p>You are given a positive integer array <code>grades</code> which represents the grades of students in a university. You would like to enter <strong>all</strong> these students into a competition in <strong>ordered</strong> non-empty groups, such that the ordering meets the following conditions:</p>
<ul>
<li>The sum of the grades of students in the <code>ith</code> group is <strong>less than</strong> the sum of the grades of students in the <code>(i + 1)th</code> group, for all groups (except the last).</li>
<li>The total number of students in the <code>ith</code> group is <strong>less than</strong> the total number of students in the <code>(i + 1)th</code> group, for all groups (except the last).</li>
</ul>
<p>Return <em>the <strong>maximum</strong> number of groups that can be formed</em>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: grades &#x3D; [10,6,12,7,3,5]</span><br><span class="line">Output: 3</span><br><span class="line">Explanation: The following is a possible way to form 3 groups of students:</span><br><span class="line">- 1st group has the students with grades &#x3D; [12]. Sum of grades: 12. Student count: 1</span><br><span class="line">- 2nd group has the students with grades &#x3D; [6,7]. Sum of grades: 6 + 7 &#x3D; 13. Student count: 2</span><br><span class="line">- 3rd group has the students with grades &#x3D; [10,3,5]. Sum of grades: 10 + 3 + 5 &#x3D; 18. Student count: 3</span><br><span class="line">It can be shown that it is not possible to form more than 3 groups.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: grades &#x3D; [8,8]</span><br><span class="line">Output: 1</span><br><span class="line">Explanation: We can only form 1 group, since forming 2 groups would lead to an equal number of students in both groups.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= grades.length &lt;= 105</code></li>
<li><code>1 &lt;= grades[i] &lt;= 105</code></li>
</ul>
<p>Medium，关键得找到数目也是增序这一条件。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">maximumGroups</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; grades)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = grades.size();</span><br><span class="line">        <span class="keyword">int</span> total = <span class="number">0</span>, cur = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span> (total + cur + <span class="number">1</span> &lt;= n) &#123;</span><br><span class="line">            total += ++cur;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> cur;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>是用数学方法进行简化</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">maximumGroups</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; grades)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> (<span class="keyword">int</span>)(<span class="built_in">sqrt</span>(grades.size() * <span class="number">2</span> + <span class="number">0.25</span>) - <span class="number">0.5</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2359-Find-Closest-Node-to-Given-Two-Nodes"><a href="#2359-Find-Closest-Node-to-Given-Two-Nodes" class="headerlink" title="2359. Find Closest Node to Given Two Nodes"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/find-closest-node-to-given-two-nodes/">2359. Find Closest Node to Given Two Nodes</a></h3><p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicating that there is a directed edge from node <code>i</code> to node <code>edges[i]</code>. If there is no outgoing edge from <code>i</code>, then <code>edges[i] == -1</code>.</p>
<p>You are also given two integers <code>node1</code> and <code>node2</code>.</p>
<p>Return <em>the <strong>index</strong> of the node that can be reached from both</em> <code>node1</code> <em>and</em> <code>node2</code><em>, such that the <strong>maximum</strong> between the distance from</em> <code>node1</code> <em>to that node, and from</em> <code>node2</code> <em>to that node is <strong>minimized</strong></em>. If there are multiple answers, return the node with the <strong>smallest</strong> index, and if no possible answer exists, return <code>-1</code>.</p>
<p>Note that <code>edges</code> may contain cycles.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-2.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: edges &#x3D; [2,2,3,-1], node1 &#x3D; 0, node2 &#x3D; 1</span><br><span class="line">Output: 2</span><br><span class="line">Explanation: The distance from node 0 to node 2 is 1, and the distance from node 1 to node 2 is 1.</span><br><span class="line">The maximum of those two distances is 1. It can be proven that we cannot get a node with a smaller maximum distance than 1, so we return node 2.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220802091148.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: edges &#x3D; [1,2,-1], node1 &#x3D; 0, node2 &#x3D; 2</span><br><span class="line">Output: 2</span><br><span class="line">Explanation: The distance from node 0 to node 2 is 2, and the distance from node 2 to itself is 0.</span><br><span class="line">The maximum of those two distances is 2. It can be proven that we cannot get a node with a smaller maximum distance than 2, so we return node 2.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == edges.length</code></li>
<li><code>2 &lt;= n &lt;= 105</code></li>
<li><code>-1 &lt;= edges[i] &lt; n</code></li>
<li><code>edges[i] != i</code></li>
<li><code>0 &lt;= node1, node2 &lt; n</code></li>
</ul>
<p>Medium，使用哈希记录BFS路径，注意路径长度相同的情况</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">closestMeetingNode</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; edges, <span class="keyword">int</span> node1, <span class="keyword">int</span> node2)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (node1 == node2) <span class="keyword">return</span> node1;</span><br><span class="line">        <span class="keyword">int</span> cur1 = node1, cur2 = node2;</span><br><span class="line">        <span class="built_in">set</span>&lt;<span class="keyword">int</span>&gt; visited1, visited2;</span><br><span class="line">        visited1.insert(cur1);</span><br><span class="line">        visited2.insert(cur2);</span><br><span class="line">        <span class="keyword">bool</span> isCircle1 = <span class="literal">false</span>, isCircle2 = <span class="literal">false</span>;</span><br><span class="line">        <span class="keyword">while</span> ((isCircle1 == <span class="literal">false</span> &amp;&amp; cur1 != <span class="number">-1</span>) || (isCircle2 == <span class="literal">false</span> &amp;&amp; cur2 != <span class="number">-1</span>)) &#123;</span><br><span class="line">            <span class="keyword">int</span> min1 = <span class="number">-1</span>, min2 = <span class="number">-1</span>;</span><br><span class="line">            <span class="keyword">if</span> (isCircle1 == <span class="literal">false</span> &amp;&amp; cur1 != <span class="number">-1</span>) &#123;</span><br><span class="line">                cur1 = edges[cur1];</span><br><span class="line">                <span class="keyword">if</span> (visited2.count(cur1)) min1 = cur1;</span><br><span class="line">                <span class="keyword">if</span> (visited1.count(cur1)) &#123;</span><br><span class="line">                    isCircle1 = <span class="literal">true</span>;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    visited1.insert(cur1);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (isCircle2 == <span class="literal">false</span> &amp;&amp; cur2 != <span class="number">-1</span>) &#123;</span><br><span class="line">                cur2 = edges[cur2];</span><br><span class="line">                <span class="keyword">if</span> (visited1.count(cur2)) min2 = cur2;</span><br><span class="line">                <span class="keyword">if</span> (visited2.count(cur2)) &#123;</span><br><span class="line">                    isCircle2 = <span class="literal">true</span>;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    visited2.insert(cur2);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (min1 &gt; <span class="number">-1</span> || min2 &gt; <span class="number">-1</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (min1 &gt; <span class="number">-1</span> &amp;&amp; min2 &gt; <span class="number">-1</span>) &#123;</span><br><span class="line">                    <span class="keyword">return</span> min(min1, min2);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span> min1 &gt; <span class="number">-1</span> ? min1 : min2;</span><br><span class="line">            &#125;</span><br><span class="line">            </span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2360-Longest-Cycle-in-a-Graph"><a href="#2360-Longest-Cycle-in-a-Graph" class="headerlink" title="2360. Longest Cycle in a Graph"></a>2360. Longest Cycle in a Graph</h3><p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicating that there is a directed edge from node <code>i</code> to node <code>edges[i]</code>. If there is no outgoing edge from node <code>i</code>, then <code>edges[i] == -1</code>.</p>
<p>Return <em>the length of the <strong>longest</strong> cycle in the graph</em>. If no cycle exists, return <code>-1</code>.</p>
<p>A cycle is a path that starts and ends at the <strong>same</strong> node.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220802091635.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: edges &#x3D; [3,3,4,2,3]</span><br><span class="line">Output: 3</span><br><span class="line">Explanation: The longest cycle in the graph is the cycle: 2 -&gt; 4 -&gt; 3 -&gt; 2.</span><br><span class="line">The length of this cycle is 3, so 3 is returned.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220802091646.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: edges &#x3D; [2,-1,3,1]</span><br><span class="line">Output: -1</span><br><span class="line">Explanation: There are no cycles in this graph.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == edges.length</code></li>
<li><code>2 &lt;= n &lt;= 105</code></li>
<li><code>-1 &lt;= edges[i] &lt; n</code></li>
<li><code>edges[i] != i</code></li>
</ul>
<p>Hard，顺序遍历未访问且入度大于1的节点</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">longestCycle</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; edges)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = edges.size();</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">inDegree</span><span class="params">(n, <span class="number">0</span>)</span></span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (edges[i] != <span class="number">-1</span>) &#123;</span><br><span class="line">                inDegree[edges[i]]++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">bool</span>&gt; <span class="title">isVisited</span><span class="params">(n, <span class="literal">true</span>)</span></span>;</span><br><span class="line">        <span class="keyword">int</span> count = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (inDegree[i] &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                isVisited[i] = <span class="literal">false</span>;</span><br><span class="line">                count++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> ans = <span class="number">-1</span>;</span><br><span class="line">        <span class="keyword">int</span> index = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span> (count &gt; <span class="number">0</span> &amp;&amp; index &lt; n) &#123;</span><br><span class="line">            <span class="keyword">while</span> (isVisited[index] == <span class="literal">true</span> &amp;&amp; index &lt; n) &#123;</span><br><span class="line">                index++;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (index == n) <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">int</span> cur = index;</span><br><span class="line">            <span class="built_in">unordered_map</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; route;</span><br><span class="line">            <span class="keyword">int</span> order = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">while</span> (cur != <span class="number">-1</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (route.count(cur)) &#123;</span><br><span class="line">                    ans = max(ans, order - route[cur]);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                route[cur] = order;</span><br><span class="line">                order++;</span><br><span class="line">                <span class="keyword">if</span> (isVisited[cur]) &#123;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    isVisited[cur] = <span class="literal">true</span>;</span><br><span class="line">                    count--;</span><br><span class="line">                &#125;</span><br><span class="line">                cur = edges[cur];</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-305"><a href="#Weekly-Contest-305" class="headerlink" title="Weekly Contest 305"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-305/">Weekly Contest 305</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220808075451.png" alt="image-20220808075445305"></p>
<h3 id="2367-Number-of-Arithmetic-Triplets"><a href="#2367-Number-of-Arithmetic-Triplets" class="headerlink" title="2367. Number of Arithmetic Triplets"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/number-of-arithmetic-triplets/">2367. Number of Arithmetic Triplets</a></h3><p>You are given a <strong>0-indexed</strong>, <strong>strictly increasing</strong> integer array <code>nums</code> and a positive integer <code>diff</code>. A triplet <code>(i, j, k)</code> is an <strong>arithmetic triplet</strong> if the following conditions are met:</p>
<ul>
<li><code>i &lt; j &lt; k</code>,</li>
<li><code>nums[j] - nums[i] == diff</code>, and</li>
<li><code>nums[k] - nums[j] == diff</code>.</li>
</ul>
<p>Return <em>the number of unique <strong>arithmetic triplets</strong>.</em></p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [0,1,4,6,7,10], diff &#x3D; 3</span><br><span class="line">Output: 2</span><br><span class="line">Explanation:</span><br><span class="line">(1, 2, 4) is an arithmetic triplet because both 7 - 4 &#x3D;&#x3D; 3 and 4 - 1 &#x3D;&#x3D; 3.</span><br><span class="line">(2, 4, 5) is an arithmetic triplet because both 10 - 7 &#x3D;&#x3D; 3 and 7 - 4 &#x3D;&#x3D; 3. </span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [4,5,6,7,8,9], diff &#x3D; 2</span><br><span class="line">Output: 2</span><br><span class="line">Explanation:</span><br><span class="line">(0, 2, 4) is an arithmetic triplet because both 8 - 6 &#x3D;&#x3D; 2 and 6 - 4 &#x3D;&#x3D; 2.</span><br><span class="line">(1, 3, 5) is an arithmetic triplet because both 9 - 7 &#x3D;&#x3D; 2 and 7 - 5 &#x3D;&#x3D; 2.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 &lt;= nums.length &lt;= 200</code></li>
<li><code>0 &lt;= nums[i] &lt;= 200</code></li>
<li><code>1 &lt;= diff &lt;= 50</code></li>
<li><code>nums</code> is <strong>strictly</strong> increasing.</li>
</ul>
<p>Easy，哈希</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">arithmeticTriplets</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="keyword">int</span> diff)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">unordered_set</span>&lt;<span class="keyword">int</span>&gt; dic;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> num: nums) &#123;</span><br><span class="line">            dic.insert(num);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; nums.size() - <span class="number">1</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (dic.count(nums[i] + diff) &amp;&amp; dic.count(nums[i] + <span class="number">2</span> * diff)) &#123;</span><br><span class="line">                res++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2368-Reachable-Nodes-With-Restrictions"><a href="#2368-Reachable-Nodes-With-Restrictions" class="headerlink" title="2368. Reachable Nodes With Restrictions"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/reachable-nodes-with-restrictions/">2368. Reachable Nodes With Restrictions</a></h3><p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p>
<p>You are given a 2D integer array <code>edges</code> of length <code>n - 1</code> where <code>edges[i] = [ai, bi]</code> indicates that there is an edge between nodes <code>ai</code> and <code>bi</code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node</em> <code>0</code> <em>without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220808075740.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: n &#x3D; 7, edges &#x3D; [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted &#x3D; [4,5]</span><br><span class="line">Output: 4</span><br><span class="line">Explanation: The diagram above shows the tree.</span><br><span class="line">We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220808075800.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: n &#x3D; 7, edges &#x3D; [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted &#x3D; [4,2,1]</span><br><span class="line">Output: 3</span><br><span class="line">Explanation: The diagram above shows the tree.</span><br><span class="line">We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= n &lt;= 105</code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 &lt;= ai, bi &lt; n</code></li>
<li><code>ai != bi</code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 &lt;= restricted.length &lt; n</code></li>
<li><code>1 &lt;= restricted[i] &lt; n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
<p>Medium，BFS</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">reachableNodes</span><span class="params">(<span class="keyword">int</span> n, <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; edges, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; restricted)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">unordered_map</span>&lt;<span class="keyword">int</span>, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; dic;</span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; &amp;edge: edges) &#123;</span><br><span class="line">            dic[edge[<span class="number">0</span>]].push_back(edge[<span class="number">1</span>]);</span><br><span class="line">            dic[edge[<span class="number">1</span>]].push_back(edge[<span class="number">0</span>]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">unordered_set</span>&lt;<span class="keyword">int</span>&gt; restricted_set;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> r: restricted) &#123;</span><br><span class="line">            restricted_set.insert(r);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">1</span>;</span><br><span class="line">        <span class="built_in">unordered_set</span>&lt;<span class="keyword">int</span>&gt; visited;</span><br><span class="line">        <span class="built_in">queue</span>&lt;<span class="keyword">int</span>&gt; q;</span><br><span class="line">        visited.insert(<span class="number">0</span>);</span><br><span class="line">        q.push(<span class="number">0</span>);</span><br><span class="line">        <span class="keyword">while</span> (!q.empty()) &#123;</span><br><span class="line">            <span class="keyword">int</span> cur = q.front();</span><br><span class="line">            q.pop();</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> node: dic[cur]) &#123;</span><br><span class="line">                <span class="keyword">if</span> (!restricted_set.count(node) &amp;&amp; !visited.count(node)) &#123;</span><br><span class="line">                    visited.insert(node);</span><br><span class="line">                    q.push(node);</span><br><span class="line">                    res++;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2369-Check-if-There-is-a-Valid-Partition-For-The-Array"><a href="#Failed-2369-Check-if-There-is-a-Valid-Partition-For-The-Array" class="headerlink" title="Failed: 2369. Check if There is a Valid Partition For The Array"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/">2369. Check if There is a Valid Partition For The Array</a></h3><p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to partition the array into one or more <strong>contiguous</strong> subarrays.</p>
<p>We call a partition of the array <strong>valid</strong> if each of the obtained subarrays satisfies <strong>one</strong> of the following conditions:</p>
<ol>
<li>The subarray consists of <strong>exactly</strong> <code>2</code> equal elements. For example, the subarray <code>[2,2]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3</code> equal elements. For example, the subarray <code>[4,4,4]</code> is good.</li>
<li>The subarray consists of <strong>exactly</strong> <code>3</code> consecutive increasing elements, that is, the difference between adjacent elements is <code>1</code>. For example, the subarray <code>[3,4,5]</code> is good, but the subarray <code>[1,3,5]</code> is not.</li>
</ol>
<p>Return <code>true</code> <em>if the array has <strong>at least</strong> one valid partition</em>. Otherwise, return <code>false</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [4,4,4,5,6]</span><br><span class="line">Output: true</span><br><span class="line">Explanation: The array can be partitioned into the subarrays [4,4] and [4,5,6].</span><br><span class="line">This partition is valid, so we return true.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [1,1,1,2]</span><br><span class="line">Output: false</span><br><span class="line">Explanation: There is no valid partition for this array.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= nums.length &lt;= 105</code></li>
<li><code>1 &lt;= nums[i] &lt;= 106</code></li>
</ul>
<p>Medium，动态规划就好，别用状态机，太复杂</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">validPartition</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = nums.size();</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">bool</span>&gt; <span class="title">dp</span><span class="params">(n + <span class="number">1</span>, <span class="literal">false</span>)</span></span>;</span><br><span class="line">        dp[<span class="number">0</span>] = <span class="literal">true</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (dp[i - <span class="number">1</span>] &amp;&amp; nums[i] == nums[i - <span class="number">1</span>]</span><br><span class="line">                ||</span><br><span class="line">                i &gt; <span class="number">1</span> &amp;&amp; dp[i - <span class="number">2</span>] &amp;&amp; nums[i] == nums[i - <span class="number">1</span>] &amp;&amp; nums[i - <span class="number">1</span>] == nums[i - <span class="number">2</span>]</span><br><span class="line">                ||</span><br><span class="line">                i &gt; <span class="number">1</span> &amp;&amp; dp[i - <span class="number">2</span>] &amp;&amp; (nums[i] == nums[i - <span class="number">1</span>] + <span class="number">1</span>) &amp;&amp; (nums[i - <span class="number">1</span>] == nums[i - <span class="number">2</span>] + <span class="number">1</span>)</span><br><span class="line">               )</span><br><span class="line">                dp[i + <span class="number">1</span>] = <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> dp[n];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2370-Longest-Ideal-Subsequence"><a href="#2370-Longest-Ideal-Subsequence" class="headerlink" title="2370. Longest Ideal Subsequence"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/longest-ideal-subsequence/">2370. Longest Ideal Subsequence</a></h3><p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p>
<ul>
<li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li>
<li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li>
</ul>
<p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</p>
<p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p>
<p><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;acfgbd&quot;, k &#x3D; 2</span><br><span class="line">Output: 4</span><br><span class="line">Explanation: The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned.</span><br><span class="line">Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;abcd&quot;, k &#x3D; 3</span><br><span class="line">Output: 4</span><br><span class="line">Explanation: The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 105</code></li>
<li><code>0 &lt;= k &lt;= 25</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
<p>Medium，动态规划+map</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">longestIdealString</span><span class="params">(<span class="built_in">string</span> s, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = s.size();</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">dp</span><span class="params">(n, <span class="number">1</span>)</span></span>;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">last</span><span class="params">(<span class="number">26</span>, <span class="number">-1</span>)</span></span>;</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">int</span> num = s[i] - <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">            <span class="keyword">int</span> min_num = max(<span class="number">0</span>, num - k);</span><br><span class="line">            <span class="keyword">int</span> max_num = min(<span class="number">25</span>, num + k);</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = min_num; j &lt;= max_num; j++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (last[j] != <span class="number">-1</span>) &#123;</span><br><span class="line">                    dp[i] = max(dp[i], dp[last[j]] + <span class="number">1</span>);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            last[num] = i;</span><br><span class="line">            res = max(res, dp[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-306"><a href="#Weekly-Contest-306" class="headerlink" title="Weekly Contest 306"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-306/">Weekly Contest 306</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220821221731.png" alt="image-20220821221725371"></p>
<h3 id="2373-Largest-Local-Values-in-a-Matrix"><a href="#2373-Largest-Local-Values-in-a-Matrix" class="headerlink" title="2373. Largest Local Values in a Matrix"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/largest-local-values-in-a-matrix/">2373. Largest Local Values in a Matrix</a></h3><p>You are given an <code>n x n</code> integer matrix <code>grid</code>.</p>
<p>Generate an integer matrix <code>maxLocal</code> of size <code>(n - 2) x (n - 2)</code> such that:</p>
<ul>
<li><code>maxLocal[i][j]</code> is equal to the <strong>largest</strong> value of the <code>3 x 3</code> matrix in <code>grid</code> centered around row <code>i + 1</code> and column <code>j + 1</code>.</li>
</ul>
<p>In other words, we want to find the largest value in every contiguous <code>3 x 3</code> matrix in <code>grid</code>.</p>
<p>Return <em>the generated matrix</em>.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220821221819.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: grid &#x3D; [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]</span><br><span class="line">Output: [[9,9],[8,6]]</span><br><span class="line">Explanation: The diagram above shows the original matrix and the generated matrix.</span><br><span class="line">Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220821221838.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: grid &#x3D; [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]</span><br><span class="line">Output: [[2,2,2],[2,2,2],[2,2,2]]</span><br><span class="line">Explanation: Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == grid.length == grid[i].length</code></li>
<li><code>3 &lt;= n &lt;= 100</code></li>
<li><code>1 &lt;= grid[i][j] &lt;= 100</code></li>
</ul>
<p>Easy，暴力</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; largestLocal(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; grid) &#123;</span><br><span class="line">        <span class="keyword">int</span> n = grid.size();</span><br><span class="line">        <span class="keyword">int</span> n_dot = n - <span class="number">2</span>;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; res (n_dot, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;(n_dot, <span class="number">0</span>));</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n_dot; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; n_dot; j++) &#123;</span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> l = <span class="number">0</span>; l &lt;= <span class="number">2</span>; l++) &#123;</span><br><span class="line">                    <span class="keyword">for</span> (<span class="keyword">int</span> m = <span class="number">0</span>; m &lt;= <span class="number">2</span>; m++) &#123;</span><br><span class="line">                        res[i][j] = max(res[i][j], grid[i + l][j + m]);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2374-Node-With-Highest-Edge-Score"><a href="#2374-Node-With-Highest-Edge-Score" class="headerlink" title="2374. Node With Highest Edge Score"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/node-with-highest-edge-score/">2374. Node With Highest Edge Score</a></h3><p>You are given a directed graph with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, where each node has <strong>exactly one</strong> outgoing edge.</p>
<p>The graph is represented by a given <strong>0-indexed</strong> integer array <code>edges</code> of length <code>n</code>, where <code>edges[i]</code> indicates that there is a <strong>directed</strong> edge from node <code>i</code> to node <code>edges[i]</code>.</p>
<p>The <strong>edge score</strong> of a node <code>i</code> is defined as the sum of the <strong>labels</strong> of all the nodes that have an edge pointing to <code>i</code>.</p>
<p>Return <em>the node with the highest <strong>edge score</strong></em>. If multiple nodes have the same <strong>edge score</strong>, return the node with the <strong>smallest</strong> index.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220821222012.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: edges &#x3D; [1,0,0,0,0,7,7,5]</span><br><span class="line">Output: 7</span><br><span class="line">Explanation:</span><br><span class="line">- The nodes 1, 2, 3 and 4 have an edge pointing to node 0. The edge score of node 0 is 1 + 2 + 3 + 4 &#x3D; 10.</span><br><span class="line">- The node 0 has an edge pointing to node 1. The edge score of node 1 is 0.</span><br><span class="line">- The node 7 has an edge pointing to node 5. The edge score of node 5 is 7.</span><br><span class="line">- The nodes 5 and 6 have an edge pointing to node 7. The edge score of node 7 is 5 + 6 &#x3D; 11.</span><br><span class="line">Node 7 has the highest edge score so return 7.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220821222024.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: edges &#x3D; [2,0,0,2]</span><br><span class="line">Output: 0</span><br><span class="line">Explanation:</span><br><span class="line">- The nodes 1 and 2 have an edge pointing to node 0. The edge score of node 0 is 1 + 2 &#x3D; 3.</span><br><span class="line">- The nodes 0 and 3 have an edge pointing to node 2. The edge score of node 2 is 0 + 3 &#x3D; 3.</span><br><span class="line">Nodes 0 and 2 both have an edge score of 3. Since node 0 has a smaller index, we return 0.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == edges.length</code></li>
<li><code>2 &lt;= n &lt;= 105</code></li>
<li><code>0 &lt;= edges[i] &lt; n</code></li>
<li><code>edges[i] != i</code></li>
</ul>
<p>Medium，统计每条边的入度</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">edgeScore</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; edges)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = edges.size();</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">long</span> <span class="keyword">long</span>&gt; <span class="title">inDegree</span><span class="params">(n, <span class="number">0</span>)</span></span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            inDegree[edges[i]] += i;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> maxCount = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (inDegree[i] &gt; inDegree[maxCount]) &#123;</span><br><span class="line">                maxCount = i;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> maxCount;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2375-Construct-Smallest-Number-From-DI-String"><a href="#Failed-2375-Construct-Smallest-Number-From-DI-String" class="headerlink" title="Failed: 2375. Construct Smallest Number From DI String"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/construct-smallest-number-from-di-string/">2375. Construct Smallest Number From DI String</a></h3><p>You are given a <strong>0-indexed</strong> string <code>pattern</code> of length <code>n</code> consisting of the characters <code>&#39;I&#39;</code> meaning <strong>increasing</strong> and <code>&#39;D&#39;</code> meaning <strong>decreasing</strong>.</p>
<p>A <strong>0-indexed</strong> string <code>num</code> of length <code>n + 1</code> is created using the following conditions:</p>
<ul>
<li><code>num</code> consists of the digits <code>&#39;1&#39;</code> to <code>&#39;9&#39;</code>, where each digit is used <strong>at most</strong> once.</li>
<li>If <code>pattern[i] == &#39;I&#39;</code>, then <code>num[i] &lt; num[i + 1]</code>.</li>
<li>If <code>pattern[i] == &#39;D&#39;</code>, then <code>num[i] &gt; num[i + 1]</code>.</li>
</ul>
<p>Return <em>the lexicographically <strong>smallest</strong> possible string</em> <code>num</code> <em>that meets the conditions.</em></p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: pattern &#x3D; &quot;IIIDIDDD&quot;</span><br><span class="line">Output: &quot;123549876&quot;</span><br><span class="line">Explanation:</span><br><span class="line">At indices 0, 1, 2, and 4 we must have that num[i] &lt; num[i+1].</span><br><span class="line">At indices 3, 5, 6, and 7 we must have that num[i] &gt; num[i+1].</span><br><span class="line">Some possible values of num are &quot;245639871&quot;, &quot;135749862&quot;, and &quot;123849765&quot;.</span><br><span class="line">It can be proven that &quot;123549876&quot; is the smallest possible num that meets the conditions.</span><br><span class="line">Note that &quot;123414321&quot; is not possible because the digit &#39;1&#39; is used more than once.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: pattern &#x3D; &quot;DDD&quot;</span><br><span class="line">Output: &quot;4321&quot;</span><br><span class="line">Explanation:</span><br><span class="line">Some possible values of num are &quot;9876&quot;, &quot;7321&quot;, and &quot;8742&quot;.</span><br><span class="line">It can be proven that &quot;4321&quot; is the smallest possible num that meets the conditions.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= pattern.length &lt;= 8</code></li>
<li><code>pattern</code> consists of only the letters <code>&#39;I&#39;</code> and <code>&#39;D&#39;</code>.</li>
</ul>
<p>Medium，使用栈</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">string</span> <span class="title">smallestNumber</span><span class="params">(<span class="built_in">string</span> pattern)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">string</span> res, stk;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt;= pattern.length(); i++) &#123;</span><br><span class="line">            stk.push_back(<span class="string">&#x27;1&#x27;</span> + i);</span><br><span class="line">            <span class="keyword">if</span> (i == pattern.length() || pattern[i] == <span class="string">&#x27;I&#x27;</span>) &#123;</span><br><span class="line">                <span class="keyword">while</span> (!stk.empty()) &#123;</span><br><span class="line">                    res.push_back(stk.back());</span><br><span class="line">                    stk.pop_back();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2376-Count-Special-Integers"><a href="#Failed-2376-Count-Special-Integers" class="headerlink" title="Failed: 2376. Count Special Integers"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/count-special-integers/">2376. Count Special Integers</a></h3><p>We call a positive integer <strong>special</strong> if all of its digits are <strong>distinct</strong>.</p>
<p>Given a <strong>positive</strong> integer <code>n</code>, return <em>the number of special integers that belong to the interval</em> <code>[1, n]</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: n &#x3D; 20</span><br><span class="line">Output: 19</span><br><span class="line">Explanation: All the integers from 1 to 20, except 11, are special. Thus, there are 19 special integers.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: n &#x3D; 5</span><br><span class="line">Output: 5</span><br><span class="line">Explanation: All the integers from 1 to 5 are special.</span><br></pre></td></tr></table></figure>

<p><strong>Example 3:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: n &#x3D; 135</span><br><span class="line">Output: 110</span><br><span class="line">Explanation: There are 110 integers from 1 to 135 that are special.</span><br><span class="line">Some of the integers that are not special are: 22, 114, and 131.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 2 * 109</code></li>
</ul>
<p>Hard，统计每位对应的数的个数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">fact</span><span class="params">(<span class="keyword">int</span> n)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (n == <span class="number">1</span> || n == <span class="number">0</span>)  <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">return</span> n * fact(n - <span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">A</span><span class="params">(<span class="keyword">int</span> m, <span class="keyword">int</span> n)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> fact(m) / fact(m - n);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">countSpecialNumbers</span><span class="params">(<span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">string</span> strN = to_string(n);</span><br><span class="line">        <span class="keyword">int</span> sz = strN.size();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">int</span> used[<span class="number">10</span>] = &#123;<span class="number">0</span>&#125;;</span><br><span class="line">        <span class="keyword">int</span> total = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; sz; i++)</span><br><span class="line">            total += <span class="number">9</span> * A(<span class="number">9</span>, i - <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; sz; i++)&#123;</span><br><span class="line">            <span class="keyword">int</span> num = strN[i] - <span class="number">48</span>;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = (i == <span class="number">0</span> ? <span class="number">1</span> : <span class="number">0</span>); j &lt; num; j++)&#123;</span><br><span class="line">                <span class="keyword">if</span> (used[j] != <span class="number">0</span>)</span><br><span class="line">                    <span class="keyword">continue</span>;</span><br><span class="line">                total += A(<span class="number">10</span> - <span class="number">1</span> - i, sz - i - <span class="number">1</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(++used[num] &gt; <span class="number">1</span>)</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">if</span> (i == sz - <span class="number">1</span>)</span><br><span class="line">                total += <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> total;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-307"><a href="#Weekly-Contest-307" class="headerlink" title="Weekly Contest 307"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-307/">Weekly Contest 307</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220821223553.png" alt="image-20220821223553499"></p>
<h3 id="2383-Minimum-Hours-of-Training-to-Win-a-Competition"><a href="#2383-Minimum-Hours-of-Training-to-Win-a-Competition" class="headerlink" title="2383. Minimum Hours of Training to Win a Competition"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/minimum-hours-of-training-to-win-a-competition/">2383. Minimum Hours of Training to Win a Competition</a></h3><p>You are entering a competition, and are given two <strong>positive</strong> integers <code>initialEnergy</code> and <code>initialExperience</code> denoting your initial energy and initial experience respectively.</p>
<p>You are also given two <strong>0-indexed</strong> integer arrays <code>energy</code> and <code>experience</code>, both of length <code>n</code>.</p>
<p>You will face <code>n</code> opponents <strong>in order</strong>. The energy and experience of the <code>ith</code> opponent is denoted by <code>energy[i]</code> and <code>experience[i]</code> respectively. When you face an opponent, you need to have both <strong>strictly</strong> greater experience and energy to defeat them and move to the next opponent if available.</p>
<p>Defeating the <code>ith</code> opponent <strong>increases</strong> your experience by <code>experience[i]</code>, but <strong>decreases</strong> your energy by <code>energy[i]</code>.</p>
<p>Before starting the competition, you can train for some number of hours. After each hour of training, you can <strong>either</strong> choose to increase your initial experience by one, or increase your initial energy by one.</p>
<p>Return <em>the <strong>minimum</strong> number of training hours required to defeat all</em> <code>n</code> <em>opponents</em>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">Input: initialEnergy &#x3D; 5, initialExperience &#x3D; 3, energy &#x3D; [1,4,3,2], experience &#x3D; [2,6,3,1]</span><br><span class="line">Output: 8</span><br><span class="line">Explanation: You can increase your energy to 11 after 6 hours of training, and your experience to 5 after 2 hours of training.</span><br><span class="line">You face the opponents in the following order:</span><br><span class="line">- You have more energy and experience than the 0th opponent so you win.</span><br><span class="line">  Your energy becomes 11 - 1 &#x3D; 10, and your experience becomes 5 + 2 &#x3D; 7.</span><br><span class="line">- You have more energy and experience than the 1st opponent so you win.</span><br><span class="line">  Your energy becomes 10 - 4 &#x3D; 6, and your experience becomes 7 + 6 &#x3D; 13.</span><br><span class="line">- You have more energy and experience than the 2nd opponent so you win.</span><br><span class="line">  Your energy becomes 6 - 3 &#x3D; 3, and your experience becomes 13 + 3 &#x3D; 16.</span><br><span class="line">- You have more energy and experience than the 3rd opponent so you win.</span><br><span class="line">  Your energy becomes 3 - 2 &#x3D; 1, and your experience becomes 16 + 1 &#x3D; 17.</span><br><span class="line">You did a total of 6 + 2 &#x3D; 8 hours of training before the competition, so we return 8.</span><br><span class="line">It can be proven that no smaller answer exists.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: initialEnergy &#x3D; 2, initialExperience &#x3D; 4, energy &#x3D; [1], experience &#x3D; [3]</span><br><span class="line">Output: 0</span><br><span class="line">Explanation: You do not need any additional energy or experience to win the competition, so we return 0.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == energy.length == experience.length</code></li>
<li><code>1 &lt;= n &lt;= 100</code></li>
<li><code>1 &lt;= initialEnergy, initialExperience, energy[i], experience[i] &lt;= 100</code></li>
</ul>
<p>Easy，题目有点绕，按照2种方式计算最小值</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">minNumberOfHours</span><span class="params">(<span class="keyword">int</span> initialEnergy, <span class="keyword">int</span> initialExperience, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; energy, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; experience)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = energy.size();</span><br><span class="line">        <span class="keyword">int</span> initialEnergyNeeded = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            initialEnergyNeeded += energy[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> res = initialEnergyNeeded &gt; initialEnergy ? initialEnergyNeeded - initialEnergy : <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> neededExp = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> <span class="built_in">exp</span> = initialExperience;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="built_in">exp</span> &lt;= experience[i]) &#123;</span><br><span class="line">                neededExp += experience[i] + <span class="number">1</span> - <span class="built_in">exp</span>;</span><br><span class="line">                <span class="built_in">exp</span> = experience[i] + <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="built_in">exp</span> += experience[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res + neededExp;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2384-Largest-Palindromic-Number"><a href="#2384-Largest-Palindromic-Number" class="headerlink" title="2384. Largest Palindromic Number"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/largest-palindromic-number/">2384. Largest Palindromic Number</a></h3><p>You are given a string <code>num</code> consisting of digits only.</p>
<p>Return <em>the <strong>largest palindromic</strong> integer (in the form of a string) that can be formed using digits taken from</em> <code>num</code>. It should not contain <strong>leading zeroes</strong>.</p>
<p><strong>Notes:</strong></p>
<ul>
<li>You do <strong>not</strong> need to use all the digits of <code>num</code>, but you must use <strong>at least</strong> one digit.</li>
<li>The digits can be reordered.</li>
</ul>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: num &#x3D; &quot;444947137&quot;</span><br><span class="line">Output: &quot;7449447&quot;</span><br><span class="line">Explanation: </span><br><span class="line">Use the digits &quot;4449477&quot; from &quot;444947137&quot; to form the palindromic integer &quot;7449447&quot;.</span><br><span class="line">It can be shown that &quot;7449447&quot; is the largest palindromic integer that can be formed.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: num &#x3D; &quot;00009&quot;</span><br><span class="line">Output: &quot;9&quot;</span><br><span class="line">Explanation: </span><br><span class="line">It can be shown that &quot;9&quot; is the largest palindromic integer that can be formed.</span><br><span class="line">Note that the integer returned should not contain leading zeroes.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= num.length &lt;= 105</code></li>
<li><code>num</code> consists of digits.</li>
</ul>
<p>Medium，按从大到小排序，注意0和最大的奇数放中间</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">string</span> <span class="title">largestPalindromic</span><span class="params">(<span class="built_in">string</span> num)</span> </span>&#123;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">count</span><span class="params">(<span class="number">10</span>, <span class="number">0</span>)</span></span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; num.size(); i++) &#123;</span><br><span class="line">            count[num[i] - <span class="string">&#x27;0&#x27;</span>]++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">string</span> res = <span class="string">&quot;&quot;</span>;</span><br><span class="line">        <span class="keyword">int</span> maxSingle = <span class="number">10</span>;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">cost</span><span class="params">(<span class="number">10</span>, <span class="number">0</span>)</span></span>;</span><br><span class="line">        <span class="keyword">bool</span> isNoZero = <span class="literal">false</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">9</span>; i &gt;= <span class="number">1</span>; i--) &#123;</span><br><span class="line">            cost[i] = count[i] / <span class="number">2</span>;</span><br><span class="line">            <span class="keyword">if</span> (cost[i] &gt; <span class="number">0</span>) isNoZero = <span class="literal">true</span>;</span><br><span class="line">            count[i] -= <span class="number">2</span> * cost[i];</span><br><span class="line">            <span class="keyword">if</span> (count[i] == <span class="number">1</span> &amp;&amp; maxSingle == <span class="number">10</span>) &#123;</span><br><span class="line">                maxSingle = i;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; cost[i]; j++) &#123;</span><br><span class="line">                res += (<span class="string">&#x27;0&#x27;</span> + i);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (isNoZero == <span class="literal">true</span>) &#123;</span><br><span class="line">            cost[<span class="number">0</span>] = count[<span class="number">0</span>] / <span class="number">2</span>;</span><br><span class="line">            count[<span class="number">0</span>] -= <span class="number">2</span> * cost[<span class="number">0</span>];</span><br><span class="line">            <span class="keyword">if</span> (count[<span class="number">0</span>] == <span class="number">1</span> &amp;&amp; maxSingle == <span class="number">10</span>) &#123;</span><br><span class="line">                maxSingle = <span class="number">0</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; cost[<span class="number">0</span>]; j++) &#123;</span><br><span class="line">                res += <span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (maxSingle != <span class="number">10</span>) &#123;</span><br><span class="line">            res += (<span class="string">&#x27;0&#x27;</span> + maxSingle);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (maxSingle == <span class="number">10</span> &amp;&amp; isNoZero == <span class="literal">false</span>) &#123;</span><br><span class="line">            res += <span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (isNoZero == <span class="literal">true</span>) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; cost[<span class="number">0</span>]; j++) &#123;</span><br><span class="line">                res += <span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= <span class="number">9</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; cost[i]; j++) &#123;</span><br><span class="line">                res += (<span class="string">&#x27;0&#x27;</span> + i);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2385-Amount-of-Time-for-Binary-Tree-to-Be-Infected"><a href="#2385-Amount-of-Time-for-Binary-Tree-to-Be-Infected" class="headerlink" title="2385. Amount of Time for Binary Tree to Be Infected"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/amount-of-time-for-binary-tree-to-be-infected/">2385. Amount of Time for Binary Tree to Be Infected</a></h3><p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220821224643.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Input: root &#x3D; [1,5,3,null,4,10,6,9,2], start &#x3D; 3</span><br><span class="line">Output: 4</span><br><span class="line">Explanation: The following nodes are infected during:</span><br><span class="line">- Minute 0: Node 3</span><br><span class="line">- Minute 1: Nodes 1, 10 and 6</span><br><span class="line">- Minute 2: Node 5</span><br><span class="line">- Minute 3: Node 4</span><br><span class="line">- Minute 4: Nodes 9 and 2</span><br><span class="line">It takes 4 minutes for the whole tree to be infected so we return 4.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220821224658.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: root &#x3D; [1], start &#x3D; 1</span><br><span class="line">Output: 0</span><br><span class="line">Explanation: At minute 0, the only node in the tree is infected so we return 0.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 105]</code>.</li>
<li><code>1 &lt;= Node.val &lt;= 105</code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
<p>Medium，2次bfs</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Definition for a binary tree node.</span></span><br><span class="line"><span class="comment"> * struct TreeNode &#123;</span></span><br><span class="line"><span class="comment"> *     int val;</span></span><br><span class="line"><span class="comment"> *     TreeNode *left;</span></span><br><span class="line"><span class="comment"> *     TreeNode *right;</span></span><br><span class="line"><span class="comment"> *     TreeNode() : val(0), left(nullptr), right(nullptr) &#123;&#125;</span></span><br><span class="line"><span class="comment"> *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) &#123;&#125;</span></span><br><span class="line"><span class="comment"> *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) &#123;&#125;</span></span><br><span class="line"><span class="comment"> * &#125;;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">amountOfTime</span><span class="params">(TreeNode* root, <span class="keyword">int</span> start)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">unordered_map</span>&lt;TreeNode*, TreeNode*&gt; dic;</span><br><span class="line">        TreeNode *pRoot;</span><br><span class="line">        <span class="built_in">queue</span>&lt;TreeNode*&gt; q;</span><br><span class="line">        q.push(root);</span><br><span class="line">        <span class="keyword">while</span> (q.size()) &#123;</span><br><span class="line">            <span class="keyword">int</span> sz = q.size();</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; sz; i++) &#123;</span><br><span class="line">                TreeNode* parent = q.front();</span><br><span class="line">                <span class="keyword">if</span> (parent-&gt;val == start) pRoot = parent;</span><br><span class="line">                q.pop();</span><br><span class="line">                <span class="keyword">if</span> (parent-&gt;left) &#123;</span><br><span class="line">                    dic[parent-&gt;left] = parent;</span><br><span class="line">                    q.push(parent-&gt;left);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (parent-&gt;right) &#123;</span><br><span class="line">                    dic[parent-&gt;right] = parent;</span><br><span class="line">                    q.push(parent-&gt;right);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">unordered_set</span>&lt;TreeNode*&gt; visited;</span><br><span class="line">        <span class="keyword">int</span> level = <span class="number">0</span>;</span><br><span class="line">        q.push(pRoot);</span><br><span class="line">        visited.insert(pRoot);</span><br><span class="line">        <span class="keyword">while</span> (q.size()) &#123;</span><br><span class="line">            <span class="keyword">int</span> sz = q.size();</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; sz; i++) &#123;</span><br><span class="line">                TreeNode* cur = q.front();</span><br><span class="line">                q.pop();</span><br><span class="line">                <span class="keyword">if</span> (dic[cur] &amp;&amp; !visited.count(dic[cur])) &#123;</span><br><span class="line">                    q.push(dic[cur]);</span><br><span class="line">                    visited.insert(dic[cur]);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (cur-&gt;left &amp;&amp; !visited.count(cur-&gt;left)) &#123;</span><br><span class="line">                    q.push(cur-&gt;left);</span><br><span class="line">                    visited.insert(cur-&gt;left);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (cur-&gt;right &amp;&amp; !visited.count(cur-&gt;right)) &#123;</span><br><span class="line">                    q.push(cur-&gt;right);</span><br><span class="line">                    visited.insert(cur-&gt;right);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            level++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> level - <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2386-Find-the-K-Sum-of-an-Array"><a href="#Failed-2386-Find-the-K-Sum-of-an-Array" class="headerlink" title="Failed: 2386. Find the K-Sum of an Array"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/find-the-k-sum-of-an-array/">2386. Find the K-Sum of an Array</a></h3><p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>. You can choose any <strong>subsequence</strong> of the array and sum all of its elements together.</p>
<p>We define the <strong>K-Sum</strong> of the array as the <code>kth</code> <strong>largest</strong> subsequence sum that can be obtained (<strong>not</strong> necessarily distinct).</p>
<p>Return <em>the K-Sum of the array</em>.</p>
<p>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p>
<p><strong>Note</strong> that the empty subsequence is considered to have a sum of <code>0</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [2,4,-2], k &#x3D; 5</span><br><span class="line">Output: 2</span><br><span class="line">Explanation: All the possible subsequence sums that we can obtain are the following sorted in decreasing order:</span><br><span class="line">- 6, 4, 4, 2, 2, 0, 0, -2.</span><br><span class="line">The 5-Sum of the array is 2.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [1,-2,3,4,-10,12], k &#x3D; 16</span><br><span class="line">Output: 10</span><br><span class="line">Explanation: The 16-Sum of the array is 10.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 &lt;= n &lt;= 105</code></li>
<li><code>-109 &lt;= nums[i] &lt;= 109</code></li>
<li><code>1 &lt;= k &lt;= min(2000, 2n)</code></li>
</ul>
<p>Hard，优先队列</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">kSum</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = nums.size();</span><br><span class="line">        <span class="comment">// sm 表示所有数的和，neg 表示所有负数之和</span></span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> sm = <span class="number">0</span>, neg = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            sm += nums[i];</span><br><span class="line">            <span class="keyword">if</span> (nums[i] &lt; <span class="number">0</span>) &#123;</span><br><span class="line">                neg += nums[i];</span><br><span class="line">                nums[i] = -nums[i];</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        sort(nums.begin(), nums.end());</span><br><span class="line"></span><br><span class="line">        <span class="comment">// k = 1 时的答案，也就是空集的和</span></span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> ans = <span class="number">0</span>;</span><br><span class="line">        <span class="built_in">priority_queue</span>&lt;<span class="built_in">pair</span>&lt;<span class="keyword">long</span> <span class="keyword">long</span>, <span class="keyword">int</span>&gt;, <span class="built_in">vector</span>&lt;<span class="built_in">pair</span>&lt;<span class="keyword">long</span> <span class="keyword">long</span>, <span class="keyword">int</span>&gt;&gt;, greater&lt;<span class="built_in">pair</span>&lt;<span class="keyword">long</span> <span class="keyword">long</span>, <span class="keyword">int</span>&gt;&gt;&gt; pq;</span><br><span class="line">        pq.push(<span class="built_in">make_pair</span>(nums[<span class="number">0</span>], <span class="number">0</span>));</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">2</span>; i &lt;= k; i++) &#123;</span><br><span class="line">            <span class="keyword">auto</span> p = pq.top();</span><br><span class="line">            pq.pop();</span><br><span class="line">            <span class="comment">// k = i 时的答案</span></span><br><span class="line">            ans = p.first;</span><br><span class="line">            <span class="keyword">if</span> (p.second == n - <span class="number">1</span>) <span class="keyword">continue</span>;</span><br><span class="line">            pq.push(<span class="built_in">make_pair</span>(p.first + nums[p.second + <span class="number">1</span>], p.second + <span class="number">1</span>));</span><br><span class="line">            pq.push(<span class="built_in">make_pair</span>(p.first - nums[p.second] + nums[p.second + <span class="number">1</span>], p.second + <span class="number">1</span>));</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> sm - (neg + ans);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-308"><a href="#Weekly-Contest-308" class="headerlink" title="Weekly Contest 308"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-308/">Weekly Contest 308</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220828220724.png" alt="image-20220828220718410"></p>
<h3 id="2389-Longest-Subsequence-With-Limited-Sum"><a href="#2389-Longest-Subsequence-With-Limited-Sum" class="headerlink" title="2389. Longest Subsequence With Limited Sum"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/longest-subsequence-with-limited-sum/">2389. Longest Subsequence With Limited Sum</a></h3><p>You are given an integer array <code>nums</code> of length <code>n</code>, and an integer array <code>queries</code> of length <code>m</code>.</p>
<p>Return <em>an array</em> <code>answer</code> <em>of length</em> <code>m</code> <em>where</em> <code>answer[i]</code> <em>is the <strong>maximum</strong> size of a <strong>subsequence</strong> that you can take from</em> <code>nums</code> <em>such that the <strong>sum</strong> of its elements is less than or equal to</em> <code>queries[i]</code>.</p>
<p>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [4,5,2,1], queries &#x3D; [3,10,21]</span><br><span class="line">Output: [2,3,4]</span><br><span class="line">Explanation: We answer the queries as follows:</span><br><span class="line">- The subsequence [2,1] has a sum less than or equal to 3. It can be proven that 2 is the maximum size of such a subsequence, so answer[0] &#x3D; 2.</span><br><span class="line">- The subsequence [4,5,1] has a sum less than or equal to 10. It can be proven that 3 is the maximum size of such a subsequence, so answer[1] &#x3D; 3.</span><br><span class="line">- The subsequence [4,5,2,1] has a sum less than or equal to 21. It can be proven that 4 is the maximum size of such a subsequence, so answer[2] &#x3D; 4.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [2,3,4,5], queries &#x3D; [1]</span><br><span class="line">Output: [0]</span><br><span class="line">Explanation: The empty subsequence is the only subsequence that has a sum less than or equal to 1, so answer[0] &#x3D; 0.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>m == queries.length</code></li>
<li><code>1 &lt;= n, m &lt;= 1000</code></li>
<li><code>1 &lt;= nums[i], queries[i] &lt;= 106</code></li>
</ul>
<p>Easy，其实比较复杂，前缀和+二分查找</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">answerQueries</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; queries)</span> </span>&#123;</span><br><span class="line">        sort(nums.begin(), nums.end());</span><br><span class="line">        <span class="keyword">int</span> n = nums.size();</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">prefix</span><span class="params">(n, <span class="number">0</span>)</span></span>;</span><br><span class="line">        prefix[<span class="number">0</span>] = nums[<span class="number">0</span>];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; n; i++) &#123;</span><br><span class="line">            prefix[i] = prefix[i - <span class="number">1</span>] + nums[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> m = queries.size();</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">res</span><span class="params">(m, <span class="number">0</span>)</span></span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; m; i++) &#123;</span><br><span class="line">            res[i] = upper_bound(prefix.begin(), prefix.end(), queries[i]) - prefix.begin();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2390-Removing-Stars-From-a-String"><a href="#2390-Removing-Stars-From-a-String" class="headerlink" title="2390. Removing Stars From a String"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/removing-stars-from-a-string/">2390. Removing Stars From a String</a></h3><p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p>
<p>In one operation, you can:</p>
<ul>
<li>Choose a star in <code>s</code>.</li>
<li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li>
</ul>
<p>Return <em>the string after <strong>all</strong> stars have been removed</em>.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The input will be generated such that the operation is always possible.</li>
<li>It can be shown that the resulting string will always be unique.</li>
</ul>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;leet**cod*e&quot;</span><br><span class="line">Output: &quot;lecoe&quot;</span><br><span class="line">Explanation: Performing the removals from left to right:</span><br><span class="line">- The closest character to the 1st star is &#39;t&#39; in &quot;leet**cod*e&quot;. s becomes &quot;lee*cod*e&quot;.</span><br><span class="line">- The closest character to the 2nd star is &#39;e&#39; in &quot;lee*cod*e&quot;. s becomes &quot;lecod*e&quot;.</span><br><span class="line">- The closest character to the 3rd star is &#39;d&#39; in &quot;lecod*e&quot;. s becomes &quot;lecoe&quot;.</span><br><span class="line">There are no more stars, so we return &quot;lecoe&quot;.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;erase*****&quot;</span><br><span class="line">Output: &quot;&quot;</span><br><span class="line">Explanation: The entire string is removed, so we return an empty string.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 105</code></li>
<li><code>s</code> consists of lowercase English letters and stars <code>*</code>.</li>
<li>The operation above can be performed on <code>s</code>.</li>
</ul>
<p>Medium，栈</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">string</span> <span class="title">removeStars</span><span class="params">(<span class="built_in">string</span> s)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">string</span> res;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">char</span> c: s) &#123;</span><br><span class="line">            <span class="keyword">if</span> (c != <span class="string">&#x27;*&#x27;</span>) &#123;</span><br><span class="line">                res.push_back(c);</span><br><span class="line">            &#125;  <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">if</span> (res.size() != <span class="number">0</span>) &#123;</span><br><span class="line">                    res.pop_back();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2391-Minimum-Amount-of-Time-to-Collect-Garbage"><a href="#2391-Minimum-Amount-of-Time-to-Collect-Garbage" class="headerlink" title="2391. Minimum Amount of Time to Collect Garbage"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/minimum-amount-of-time-to-collect-garbage/">2391. Minimum Amount of Time to Collect Garbage</a></h3><p>You are given a <strong>0-indexed</strong> array of strings <code>garbage</code> where <code>garbage[i]</code> represents the assortment of garbage at the <code>ith</code> house. <code>garbage[i]</code> consists only of the characters <code>&#39;M&#39;</code>, <code>&#39;P&#39;</code> and <code>&#39;G&#39;</code> representing one unit of metal, paper and glass garbage respectively. Picking up <strong>one</strong> unit of any type of garbage takes <code>1</code> minute.</p>
<p>You are also given a <strong>0-indexed</strong> integer array <code>travel</code> where <code>travel[i]</code> is the number of minutes needed to go from house <code>i</code> to house <code>i + 1</code>.</p>
<p>There are three garbage trucks in the city, each responsible for picking up one type of garbage. Each garbage truck starts at house <code>0</code> and must visit each house <strong>in order</strong>; however, they do <strong>not</strong> need to visit every house.</p>
<p>Only <strong>one</strong> garbage truck may be used at any given moment. While one truck is driving or picking up garbage, the other two trucks <strong>cannot</strong> do anything.</p>
<p>Return <em>the <strong>minimum</strong> number of minutes needed to pick up all the garbage.</em></p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">Input: garbage &#x3D; [&quot;G&quot;,&quot;P&quot;,&quot;GP&quot;,&quot;GG&quot;], travel &#x3D; [2,4,3]</span><br><span class="line">Output: 21</span><br><span class="line">Explanation:</span><br><span class="line">The paper garbage truck:</span><br><span class="line">1. Travels from house 0 to house 1</span><br><span class="line">2. Collects the paper garbage at house 1</span><br><span class="line">3. Travels from house 1 to house 2</span><br><span class="line">4. Collects the paper garbage at house 2</span><br><span class="line">Altogether, it takes 8 minutes to pick up all the paper garbage.</span><br><span class="line">The glass garbage truck:</span><br><span class="line">1. Collects the glass garbage at house 0</span><br><span class="line">2. Travels from house 0 to house 1</span><br><span class="line">3. Travels from house 1 to house 2</span><br><span class="line">4. Collects the glass garbage at house 2</span><br><span class="line">5. Travels from house 2 to house 3</span><br><span class="line">6. Collects the glass garbage at house 3</span><br><span class="line">Altogether, it takes 13 minutes to pick up all the glass garbage.</span><br><span class="line">Since there is no metal garbage, we do not need to consider the metal garbage truck.</span><br><span class="line">Therefore, it takes a total of 8 + 13 &#x3D; 21 minutes to collect all the garbage.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: garbage &#x3D; [&quot;MMM&quot;,&quot;PGM&quot;,&quot;GP&quot;], travel &#x3D; [3,10]</span><br><span class="line">Output: 37</span><br><span class="line">Explanation:</span><br><span class="line">The metal garbage truck takes 7 minutes to pick up all the metal garbage.</span><br><span class="line">The paper garbage truck takes 15 minutes to pick up all the paper garbage.</span><br><span class="line">The glass garbage truck takes 15 minutes to pick up all the glass garbage.</span><br><span class="line">It takes a total of 7 + 15 + 15 &#x3D; 37 minutes to collect all the garbage.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= garbage.length &lt;= 105</code></li>
<li><code>garbage[i]</code> consists of only the letters <code>&#39;M&#39;</code>, <code>&#39;P&#39;</code>, and <code>&#39;G&#39;</code>.</li>
<li><code>1 &lt;= garbage[i].length &lt;= 10</code></li>
<li><code>travel.length == garbage.length - 1</code></li>
<li><code>1 &lt;= travel[i] &lt;= 100</code></li>
</ul>
<p>Medium，哈希</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">garbageCollection</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt;&amp; garbage, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; travel)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = garbage.size();</span><br><span class="line">        <span class="keyword">int</span> mTravel = <span class="number">0</span>, pTravel = <span class="number">0</span>, gTravel = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> mPick = <span class="number">0</span>, pPick = <span class="number">0</span>, gPick = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> totalTravel = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">char</span> c: garbage[i]) &#123;</span><br><span class="line">                <span class="keyword">if</span> (c == <span class="string">&#x27;M&#x27;</span>) &#123;</span><br><span class="line">                    mPick++;</span><br><span class="line">                    mTravel = totalTravel;</span><br><span class="line">                &#125; <span class="keyword">else</span> <span class="keyword">if</span> (c == <span class="string">&#x27;P&#x27;</span>) &#123;</span><br><span class="line">                    pPick++;</span><br><span class="line">                    pTravel = totalTravel;</span><br><span class="line">                &#125; <span class="keyword">else</span> <span class="keyword">if</span> (c == <span class="string">&#x27;G&#x27;</span>) &#123;</span><br><span class="line">                    gPick++;</span><br><span class="line">                    gTravel = totalTravel;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (i != n - <span class="number">1</span>) &#123;</span><br><span class="line">                totalTravel += travel[i];</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> mTravel + pTravel + gTravel + mPick + pPick + gPick;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2392-Build-a-Matrix-With-Conditions"><a href="#Failed-2392-Build-a-Matrix-With-Conditions" class="headerlink" title="Failed: 2392. Build a Matrix With Conditions"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/build-a-matrix-with-conditions/">2392. Build a Matrix With Conditions</a></h3><p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [abovei, belowi]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <code>m</code> where <code>colConditions[i] = [lefti, righti]</code>.</li>
</ul>
<p>The two arrays contain integers from <code>1</code> to <code>k</code>.</p>
<p>You have to build a <code>k x k</code> matrix that contains each of the numbers from <code>1</code> to <code>k</code> <strong>exactly once</strong>. The remaining cells should have the value <code>0</code>.</p>
<p>The matrix should also satisfy the following conditions:</p>
<ul>
<li>The number <code>abovei</code> should appear in a <strong>row</strong> that is strictly <strong>above</strong> the row at which the number <code>belowi</code> appears for all <code>i</code> from <code>0</code> to <code>n - 1</code>.</li>
<li>The number <code>lefti</code> should appear in a <strong>column</strong> that is strictly <strong>left</strong> of the column at which the number <code>righti</code> appears for all <code>i</code> from <code>0</code> to <code>m - 1</code>.</li>
</ul>
<p>Return *<strong>any</strong> matrix that satisfies the conditions*. If no answer exists, return an empty matrix.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220828223454.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Input: k &#x3D; 3, rowConditions &#x3D; [[1,2],[3,2]], colConditions &#x3D; [[2,1],[3,2]]</span><br><span class="line">Output: [[3,0,0],[0,0,1],[0,2,0]]</span><br><span class="line">Explanation: The diagram above shows a valid example of a matrix that satisfies all the conditions.</span><br><span class="line">The row conditions are the following:</span><br><span class="line">- Number 1 is in row 1, and number 2 is in row 2, so 1 is above 2 in the matrix.</span><br><span class="line">- Number 3 is in row 0, and number 2 is in row 2, so 3 is above 2 in the matrix.</span><br><span class="line">The column conditions are the following:</span><br><span class="line">- Number 2 is in column 1, and number 1 is in column 2, so 2 is left of 1 in the matrix.</span><br><span class="line">- Number 3 is in column 0, and number 2 is in column 1, so 3 is left of 2 in the matrix.</span><br><span class="line">Note that there may be multiple correct answers.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: k &#x3D; 3, rowConditions &#x3D; [[1,2],[2,3],[3,1],[2,3]], colConditions &#x3D; [[2,1]]</span><br><span class="line">Output: []</span><br><span class="line">Explanation: From the first two conditions, 3 has to be below 1 but the third conditions needs 3 to be above 1 to be satisfied.</span><br><span class="line">No matrix can satisfy all the conditions, so we return the empty matrix.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= k &lt;= 400</code></li>
<li><code>1 &lt;= rowConditions.length, colConditions.length &lt;= 104</code></li>
<li><code>rowConditions[i].length == colConditions[i].length == 2</code></li>
<li><code>1 &lt;= abovei, belowi, lefti, righti &lt;= k</code></li>
<li><code>abovei != belowi</code></li>
<li><code>lefti != righti</code></li>
</ul>
<p>Hard，拓扑排序</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; buildMatrix(<span class="keyword">int</span> k, <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; rowConditions, <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; colConditions) &#123;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; order1 = generate_topological_sort(rowConditions, k);</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; order2 = generate_topological_sort(colConditions, k);</span><br><span class="line">        <span class="keyword">if</span> (order1.size() &lt; k || order2.size() &lt; k) <span class="keyword">return</span> &#123;&#125;;</span><br><span class="line">        <span class="built_in">unordered_map</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; m;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; k; i++) m[order2[i]] = i;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; ans(k, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;(k, <span class="number">0</span>));</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; k; i++) &#123;</span><br><span class="line">            ans[i][m[order1[i]]] = order1[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">generate_topological_sort</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; &amp;A, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        vector&lt;int&gt; deg(k, 0), order;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; graph(k, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;(<span class="number">0</span>));</span><br><span class="line">        <span class="built_in">queue</span>&lt;<span class="keyword">int</span>&gt; q;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">auto</span> &amp;c: A) &#123;</span><br><span class="line">            graph[c[<span class="number">0</span>] - <span class="number">1</span>].push_back(c[<span class="number">1</span>] - <span class="number">1</span>);</span><br><span class="line">            deg[c[<span class="number">1</span>] - <span class="number">1</span>]++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; k; i++) </span><br><span class="line">            <span class="keyword">if</span> (!deg[i]) q.push(i);</span><br><span class="line">        <span class="keyword">while</span> (!q.empty()) &#123;</span><br><span class="line">            <span class="keyword">int</span> x = q.front(); q.pop();</span><br><span class="line">            order.push_back(x + <span class="number">1</span>);</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span>&amp; y: graph[x]) </span><br><span class="line">                <span class="keyword">if</span> (--deg[y] == <span class="number">0</span>) </span><br><span class="line">                    q.push(y);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> order;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-309"><a href="#Weekly-Contest-309" class="headerlink" title="Weekly Contest 309"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-309/">Weekly Contest 309</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220904223705.png" alt="image-20220904223659259"></p>
<h3 id="2399-Check-Distances-Between-Same-Letters"><a href="#2399-Check-Distances-Between-Same-Letters" class="headerlink" title="2399. Check Distances Between Same Letters"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/check-distances-between-same-letters/">2399. Check Distances Between Same Letters</a></h3><p>You are given a <strong>0-indexed</strong> string <code>s</code> consisting of only lowercase English letters, where each letter in <code>s</code> appears <strong>exactly</strong> <strong>twice</strong>. You are also given a <strong>0-indexed</strong> integer array <code>distance</code> of length <code>26</code>.</p>
<p>Each letter in the alphabet is numbered from <code>0</code> to <code>25</code> (i.e. <code>&#39;a&#39; -&gt; 0</code>, <code>&#39;b&#39; -&gt; 1</code>, <code>&#39;c&#39; -&gt; 2</code>, … , <code>&#39;z&#39; -&gt; 25</code>).</p>
<p>In a <strong>well-spaced</strong> string, the number of letters between the two occurrences of the <code>ith</code> letter is <code>distance[i]</code>. If the <code>ith</code> letter does not appear in <code>s</code>, then <code>distance[i]</code> can be <strong>ignored</strong>.</p>
<p>Return <code>true</code> <em>if</em> <code>s</code> <em>is a <strong>well-spaced</strong> string, otherwise return</em> <code>false</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;abaccb&quot;, distance &#x3D; [1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]</span><br><span class="line">Output: true</span><br><span class="line">Explanation:</span><br><span class="line">- &#39;a&#39; appears at indices 0 and 2 so it satisfies distance[0] &#x3D; 1.</span><br><span class="line">- &#39;b&#39; appears at indices 1 and 5 so it satisfies distance[1] &#x3D; 3.</span><br><span class="line">- &#39;c&#39; appears at indices 3 and 4 so it satisfies distance[2] &#x3D; 0.</span><br><span class="line">Note that distance[3] &#x3D; 5, but since &#39;d&#39; does not appear in s, it can be ignored.</span><br><span class="line">Return true because s is a well-spaced string.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;aa&quot;, distance &#x3D; [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]</span><br><span class="line">Output: false</span><br><span class="line">Explanation:</span><br><span class="line">- &#39;a&#39; appears at indices 0 and 1 so there are zero letters between them.</span><br><span class="line">Because distance[0] &#x3D; 1, s is not a well-spaced string.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= s.length &lt;= 52</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
<li>Each letter appears in <code>s</code> exactly twice.</li>
<li><code>distance.length == 26</code></li>
<li><code>0 &lt;= distance[i] &lt;= 50</code></li>
</ul>
<p>Easy，保存每个字母出现的位置</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">checkDistances</span><span class="params">(<span class="built_in">string</span> s, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; distance)</span> </span>&#123;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">pre</span><span class="params">(<span class="number">26</span>, <span class="number">-1</span>)</span></span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; s.size(); i++) &#123;</span><br><span class="line">            <span class="keyword">int</span> num = s[i] - <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">            <span class="keyword">if</span> (pre[num] == <span class="number">-1</span>) &#123;</span><br><span class="line">                pre[num] = i;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">if</span> (i - pre[num] - <span class="number">1</span> != distance[num]) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2400-Number-of-Ways-to-Reach-a-Position-After-Exactly-k-Steps"><a href="#Failed-2400-Number-of-Ways-to-Reach-a-Position-After-Exactly-k-Steps" class="headerlink" title="Failed: 2400. Number of Ways to Reach a Position After Exactly k Steps"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/number-of-ways-to-reach-a-position-after-exactly-k-steps/">2400. Number of Ways to Reach a Position After Exactly k Steps</a></h3><p>You are given two <strong>positive</strong> integers <code>startPos</code> and <code>endPos</code>. Initially, you are standing at position <code>startPos</code> on an <strong>infinite</strong> number line. With one step, you can move either one position to the left, or one position to the right.</p>
<p>Given a positive integer <code>k</code>, return <em>the number of <strong>different</strong> ways to reach the position</em> <code>endPos</code> <em>starting from</em> <code>startPos</code><em>, such that you perform <strong>exactly</strong></em> <code>k</code> <em>steps</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>109 + 7</code>.</p>
<p>Two ways are considered different if the order of the steps made is not exactly the same.</p>
<p><strong>Note</strong> that the number line includes negative integers.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: startPos &#x3D; 1, endPos &#x3D; 2, k &#x3D; 3</span><br><span class="line">Output: 3</span><br><span class="line">Explanation: We can reach position 2 from 1 in exactly 3 steps in three ways:</span><br><span class="line">- 1 -&gt; 2 -&gt; 3 -&gt; 2.</span><br><span class="line">- 1 -&gt; 2 -&gt; 1 -&gt; 2.</span><br><span class="line">- 1 -&gt; 0 -&gt; 1 -&gt; 2.</span><br><span class="line">It can be proven that no other way is possible, so we return 3.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: startPos &#x3D; 2, endPos &#x3D; 5, k &#x3D; 10</span><br><span class="line">Output: 0</span><br><span class="line">Explanation: It is impossible to reach position 5 from position 2 in exactly 10 steps.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= startPos, endPos, k &lt;= 1000</code></li>
</ul>
<p>Medium，求组合数使用乘除法有点坑，实在做不出来建议动态规划</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> mod = <span class="number">1e9</span> + <span class="number">7</span>;</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">numberOfWays</span><span class="params">(<span class="keyword">int</span> startPos, <span class="keyword">int</span> endPos, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ((k - (endPos - startPos)) % <span class="number">2</span> != <span class="number">0</span> || <span class="built_in">abs</span>(k) &lt; <span class="built_in">abs</span>(endPos - startPos)) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> pos = (endPos - startPos + k) / <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">int</span> neg = k - pos;</span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> <span class="keyword">int</span> res = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">int</span> a = pos &gt; neg ? neg : pos;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; a; i++) &#123;</span><br><span class="line">            res = res * (k - i) % mod;</span><br><span class="line">            res = res * inv(i + <span class="number">1</span>) % mod;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">long</span> <span class="title">inv</span><span class="params">(<span class="keyword">long</span> a)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (a == <span class="number">1</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">return</span> (mod - mod / a) * inv(mod % a) % mod;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2401-Longest-Nice-Subarray"><a href="#Failed-2401-Longest-Nice-Subarray" class="headerlink" title="Failed: 2401. Longest Nice Subarray"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/longest-nice-subarray/">2401. Longest Nice Subarray</a></h3><p>You are given an array <code>nums</code> consisting of <strong>positive</strong> integers.</p>
<p>We call a subarray of <code>nums</code> <strong>nice</strong> if the bitwise <strong>AND</strong> of every pair of elements that are in <strong>different</strong> positions in the subarray is equal to <code>0</code>.</p>
<p>Return <em>the length of the <strong>longest</strong> nice subarray</em>.</p>
<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>
<p><strong>Note</strong> that subarrays of length <code>1</code> are always considered nice.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [1,3,8,48,10]</span><br><span class="line">Output: 3</span><br><span class="line">Explanation: The longest nice subarray is [3,8,48]. This subarray satisfies the conditions:</span><br><span class="line">- 3 AND 8 &#x3D; 0.</span><br><span class="line">- 3 AND 48 &#x3D; 0.</span><br><span class="line">- 8 AND 48 &#x3D; 0.</span><br><span class="line">It can be proven that no longer nice subarray can be obtained, so we return 3.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [3,1,5,11,13]</span><br><span class="line">Output: 1</span><br><span class="line">Explanation: The length of the longest nice subarray is 1. Any subarray of length 1 can be chosen.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 105</code></li>
<li><code>1 &lt;= nums[i] &lt;= 109</code></li>
</ul>
<p>Medium，滑动窗口+位运算，挨个统计位会超时</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">longestNiceSubarray</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> AND = <span class="number">0</span>, i = <span class="number">0</span>, res = <span class="number">0</span>, n = nums.size();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; n; j++) &#123;</span><br><span class="line">            <span class="keyword">while</span> ((AND &amp; nums[j]) &gt; <span class="number">0</span>)</span><br><span class="line">                AND ^= nums[i++];</span><br><span class="line">            AND |= nums[j];</span><br><span class="line">            res = max(res, j - i + <span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2402-Meeting-Rooms-III"><a href="#2402-Meeting-Rooms-III" class="headerlink" title="2402. Meeting Rooms III"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/meeting-rooms-iii/">2402. Meeting Rooms III</a></h3><p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [starti, endi]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[starti, endi)</code>. All the values of <code>starti</code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return <em>the <strong>number</strong> of the room that held the most meetings.</em> If there are multiple rooms, return <em>the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Input: n &#x3D; 2, meetings &#x3D; [[0,10],[1,5],[2,7],[3,4]]</span><br><span class="line">Output: 0</span><br><span class="line">Explanation:</span><br><span class="line">- At time 0, both rooms are not being used. The first meeting starts in room 0.</span><br><span class="line">- At time 1, only room 1 is not being used. The second meeting starts in room 1.</span><br><span class="line">- At time 2, both rooms are being used. The third meeting is delayed.</span><br><span class="line">- At time 3, both rooms are being used. The fourth meeting is delayed.</span><br><span class="line">- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).</span><br><span class="line">- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).</span><br><span class="line">Both rooms 0 and 1 held 2 meetings, so we return 0. </span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">Input: n &#x3D; 3, meetings &#x3D; [[1,20],[2,10],[3,5],[4,9],[6,8]]</span><br><span class="line">Output: 1</span><br><span class="line">Explanation:</span><br><span class="line">- At time 1, all three rooms are not being used. The first meeting starts in room 0.</span><br><span class="line">- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.</span><br><span class="line">- At time 3, only room 2 is not being used. The third meeting starts in room 2.</span><br><span class="line">- At time 4, all three rooms are being used. The fourth meeting is delayed.</span><br><span class="line">- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).</span><br><span class="line">- At time 6, all three rooms are being used. The fifth meeting is delayed.</span><br><span class="line">- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).</span><br><span class="line">Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1. </span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 100</code></li>
<li><code>1 &lt;= meetings.length &lt;= 105</code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 &lt;= starti &lt; endi &lt;= 5 * 105</code></li>
<li>All the values of <code>starti</code> are <strong>unique</strong>.</li>
</ul>
<p>Hard，优先队列</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">compare</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; v1, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; v2)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> v1[<span class="number">0</span>] &lt; v2[<span class="number">0</span>];</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">mostBooked</span><span class="params">(<span class="keyword">int</span> n, <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; meetings)</span> </span>&#123;</span><br><span class="line">        <span class="comment">/* Sort the meetings based on start_time */</span></span><br><span class="line">        sort(meetings.begin(), meetings.end(), compare);</span><br><span class="line">        </span><br><span class="line">        <span class="comment">/* Create a priority queue for engaged rooms. Each node of PQ will store &#123;current_meeting_ending_time, room_number&#125; */</span></span><br><span class="line">        <span class="built_in">priority_queue</span>&lt;<span class="built_in">pair</span>&lt;<span class="keyword">long</span> <span class="keyword">long</span>, <span class="keyword">int</span>&gt;, <span class="built_in">vector</span>&lt;<span class="built_in">pair</span>&lt;<span class="keyword">long</span> <span class="keyword">long</span>, <span class="keyword">int</span>&gt;&gt;, greater&lt;<span class="built_in">pair</span>&lt;<span class="keyword">long</span> <span class="keyword">long</span>, <span class="keyword">int</span>&gt;&gt;&gt; engaged;</span><br><span class="line">        </span><br><span class="line">        <span class="comment">/* Create a priority queue for non-engaged rooms. Each node of PQ will store &#123;room_number&#125; */</span></span><br><span class="line">        <span class="built_in">priority_queue</span>&lt;<span class="keyword">int</span>, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;, greater&lt;<span class="keyword">int</span>&gt;&gt; unused;</span><br><span class="line">        </span><br><span class="line">        <span class="comment">/* Frequency map to store the frequency of meetings in each room */</span></span><br><span class="line">        <span class="built_in">unordered_map</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; freq;</span><br><span class="line">        </span><br><span class="line">        <span class="comment">/* Currently all the rooms are mepty */</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            unused.push(i);</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">auto</span> x:meetings) &#123;</span><br><span class="line">            <span class="keyword">int</span> s = x[<span class="number">0</span>], e = x[<span class="number">1</span>];</span><br><span class="line">            </span><br><span class="line">            <span class="comment">/* Move the meeting rooms in engaged, with ending_time &lt;= s, to unused */</span> </span><br><span class="line">            <span class="keyword">while</span>(engaged.size() &gt; <span class="number">0</span> &amp;&amp; engaged.top().first &lt;= s) &#123;</span><br><span class="line">                <span class="keyword">int</span> room = engaged.top().second;</span><br><span class="line">                engaged.pop();</span><br><span class="line">                </span><br><span class="line">                unused.push(room);</span><br><span class="line">            &#125;</span><br><span class="line">            </span><br><span class="line">            <span class="comment">/* If there are multiple empty rooms, choose the one with lower room_number */</span></span><br><span class="line">            <span class="keyword">if</span> (unused.size() &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">int</span> room = unused.top();</span><br><span class="line">                unused.pop();</span><br><span class="line">                </span><br><span class="line">                freq[<span class="built_in">abs</span>(room)]++;</span><br><span class="line">                </span><br><span class="line">                <span class="comment">/* Mark the room as engaged */</span></span><br><span class="line">                engaged.push(&#123;e, room&#125;);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">/* If there are no empty rooms, wait for the engaged room with nearest ending time to empty */</span></span><br><span class="line">            <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="built_in">pair</span>&lt;<span class="keyword">long</span> <span class="keyword">long</span>, <span class="keyword">int</span>&gt; topmost = engaged.top();</span><br><span class="line">                engaged.pop();</span><br><span class="line">            </span><br><span class="line">                freq[<span class="built_in">abs</span>(topmost.second)] += <span class="number">1</span>;</span><br><span class="line">                </span><br><span class="line">                <span class="comment">/* Since duration has to be the same, the newEnd will be sum(end_time_of_the_prev_meeting, duration) */</span> </span><br><span class="line">                <span class="keyword">long</span> <span class="keyword">long</span> newEnd = topmost.first;</span><br><span class="line">                newEnd += (e - s);</span><br><span class="line">                </span><br><span class="line">                <span class="comment">/* Mark the room as engaged */</span></span><br><span class="line">                engaged.push(&#123;newEnd, topmost.second&#125;);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="comment">/* Find the lowest room_number with maximum frequency */</span></span><br><span class="line">        <span class="keyword">int</span> maxi = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (freq[i] &gt; freq[maxi])</span><br><span class="line">                maxi = i;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> maxi;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-310"><a href="#Weekly-Contest-310" class="headerlink" title="Weekly Contest 310"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-310/">Weekly Contest 310</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220919090522.png" alt="image-20220919090522788"></p>
<h3 id="2404-Most-Frequent-Even-Element"><a href="#2404-Most-Frequent-Even-Element" class="headerlink" title="2404. Most Frequent Even Element"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/most-frequent-even-element/">2404. Most Frequent Even Element</a></h3><p>Given an integer array <code>nums</code>, return <em>the most frequent even element</em>.</p>
<p>If there is a tie, return the <strong>smallest</strong> one. If there is no such element, return <code>-1</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [0,1,2,2,4,4,1]</span><br><span class="line">Output: 2</span><br><span class="line">Explanation:</span><br><span class="line">The even elements are 0, 2, and 4. Of these, 2 and 4 appear the most.</span><br><span class="line">We return the smallest one, which is 2.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [4,4,4,9,2,4]</span><br><span class="line">Output: 4</span><br><span class="line">Explanation: 4 is the even element appears the most.</span><br></pre></td></tr></table></figure>

<p><strong>Example 3:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [29,47,21,41,13,37,25,7]</span><br><span class="line">Output: -1</span><br><span class="line">Explanation: There is no even element.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 2000</code></li>
<li><code>0 &lt;= nums[i] &lt;= 105</code></li>
</ul>
<p>Easy，计数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">mostFrequentEven</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">unordered_map</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; count;</span><br><span class="line">        <span class="keyword">int</span> maxCount = <span class="number">-1</span>, minNum = <span class="number">-1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> num: nums) &#123;</span><br><span class="line">            <span class="keyword">if</span> (num % <span class="number">2</span> == <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (count[num]) &#123;</span><br><span class="line">                    count[num]++;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    count[num] = <span class="number">1</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (count[num] &gt; maxCount) &#123;</span><br><span class="line">                    maxCount = count[num];</span><br><span class="line">                    minNum = num;</span><br><span class="line">                &#125; <span class="keyword">else</span> <span class="keyword">if</span> (count[num] == maxCount &amp;&amp; num &lt; minNum) &#123;</span><br><span class="line">                    minNum = num;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> minNum;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2405-Optimal-Partition-of-String"><a href="#2405-Optimal-Partition-of-String" class="headerlink" title="2405. Optimal Partition of String"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/optimal-partition-of-string/">2405. Optimal Partition of String</a></h3><p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;abacaba&quot;</span><br><span class="line">Output: 4</span><br><span class="line">Explanation:</span><br><span class="line">Two possible partitions are (&quot;a&quot;,&quot;ba&quot;,&quot;cab&quot;,&quot;a&quot;) and (&quot;ab&quot;,&quot;a&quot;,&quot;ca&quot;,&quot;ba&quot;).</span><br><span class="line">It can be shown that 4 is the minimum number of substrings needed.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;ssssss&quot;</span><br><span class="line">Output: 6</span><br><span class="line">Explanation:</span><br><span class="line">The only valid partition is (&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;).</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 105</code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
<p>Medium，双指针</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">partitionString</span><span class="params">(<span class="built_in">string</span> s)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">1</span>;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">bool</span>&gt; <span class="title">window</span><span class="params">(<span class="number">26</span>, <span class="literal">false</span>)</span></span>;</span><br><span class="line">        window[s[<span class="number">0</span>] - <span class="string">&#x27;a&#x27;</span>] = <span class="literal">true</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; s.size(); i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (window[s[i] - <span class="string">&#x27;a&#x27;</span>] == <span class="literal">true</span>) &#123;</span><br><span class="line">                res++;</span><br><span class="line">                window.assign(<span class="number">26</span>, <span class="literal">false</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            window[s[i] - <span class="string">&#x27;a&#x27;</span>] = <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2406-Divide-Intervals-Into-Minimum-Number-of-Groups"><a href="#2406-Divide-Intervals-Into-Minimum-Number-of-Groups" class="headerlink" title="2406. Divide Intervals Into Minimum Number of Groups"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/divide-intervals-into-minimum-number-of-groups/">2406. Divide Intervals Into Minimum Number of Groups</a></h3><p>You are given a 2D integer array <code>intervals</code> where <code>intervals[i] = [lefti, righti]</code> represents the <strong>inclusive</strong> interval <code>[lefti, righti]</code>.</p>
<p>You have to divide the intervals into one or more <strong>groups</strong> such that each interval is in <strong>exactly</strong> one group, and no two intervals that are in the same group <strong>intersect</strong> each other.</p>
<p>Return <em>the <strong>minimum</strong> number of groups you need to make</em>.</p>
<p>Two intervals <strong>intersect</strong> if there is at least one common number between them. For example, the intervals <code>[1, 5]</code> and <code>[5, 8]</code> intersect.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: intervals &#x3D; [[5,10],[6,8],[1,5],[2,3],[1,10]]</span><br><span class="line">Output: 3</span><br><span class="line">Explanation: We can divide the intervals into the following groups:</span><br><span class="line">- Group 1: [1, 5], [6, 8].</span><br><span class="line">- Group 2: [2, 3], [5, 10].</span><br><span class="line">- Group 3: [1, 10].</span><br><span class="line">It can be proven that it is not possible to divide the intervals into fewer than 3 groups.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: intervals &#x3D; [[1,3],[5,6],[8,10],[11,13]]</span><br><span class="line">Output: 1</span><br><span class="line">Explanation: None of the intervals overlap, so we can put all of them in one group.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= intervals.length &lt;= 105</code></li>
<li><code>intervals[i].length == 2</code></li>
<li><code>1 &lt;= lefti &lt;= righti &lt;= 106</code></li>
</ul>
<p>Medium，排序+贪心（利用优先队列）</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">minGroups</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; intervals)</span> </span>&#123;</span><br><span class="line">        sort(intervals.begin(), intervals.end(), [](<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; v1, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; v2)&#123;</span><br><span class="line">            <span class="keyword">if</span> (v1[<span class="number">0</span>] != v2[<span class="number">0</span>]) <span class="keyword">return</span> v1[<span class="number">0</span>] &lt; v2[<span class="number">0</span>];</span><br><span class="line">            <span class="keyword">return</span> v1[<span class="number">1</span>] &lt; v2[<span class="number">1</span>]; </span><br><span class="line">        &#125;);</span><br><span class="line">        <span class="built_in">priority_queue</span>&lt;<span class="keyword">int</span>, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;, greater&lt;<span class="keyword">int</span>&gt;&gt; pq;</span><br><span class="line">        pq.push(intervals[<span class="number">0</span>][<span class="number">1</span>]);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; intervals.size(); i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (pq.top() &lt; intervals[i][<span class="number">0</span>]) &#123;</span><br><span class="line">                pq.pop();</span><br><span class="line">                pq.push(intervals[i][<span class="number">1</span>]);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                pq.push(intervals[i][<span class="number">1</span>]);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> pq.size();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>看到大佬的解法，开始+1，结束-1</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">minGroups</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; intervals)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; A;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">auto</span>&amp; v : intervals) &#123;</span><br><span class="line">            A.push_back(&#123;v[<span class="number">0</span>], <span class="number">1</span>&#125;);</span><br><span class="line">            A.push_back(&#123;v[<span class="number">1</span>] + <span class="number">1</span>, <span class="number">-1</span>&#125;);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">0</span>, cur = <span class="number">0</span>;</span><br><span class="line">        sort(A.begin(), A.end());</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">auto</span>&amp; v : A)</span><br><span class="line">            res = max(res, cur += v[<span class="number">1</span>]);</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2407-Longest-Increasing-Subsequence-II"><a href="#Failed-2407-Longest-Increasing-Subsequence-II" class="headerlink" title="Failed: 2407. Longest Increasing Subsequence II"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/longest-increasing-subsequence-ii/">2407. Longest Increasing Subsequence II</a></h3><p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>Find the longest subsequence of <code>nums</code> that meets the following requirements:</p>
<ul>
<li>The subsequence is <strong>strictly increasing</strong> and</li>
<li>The difference between adjacent elements in the subsequence is <strong>at most</strong> <code>k</code>.</li>
</ul>
<p>Return <em>the length of the <strong>longest</strong> <strong>subsequence</strong> that meets the requirements.</em></p>
<p>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [4,2,1,4,3,4,5,8,15], k &#x3D; 3</span><br><span class="line">Output: 5</span><br><span class="line">Explanation:</span><br><span class="line">The longest subsequence that meets the requirements is [1,3,4,5,8].</span><br><span class="line">The subsequence has a length of 5, so we return 5.</span><br><span class="line">Note that the subsequence [1,3,4,5,8,15] does not meet the requirements because 15 - 8 &#x3D; 7 is larger than 3.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [7,4,5,1,8,12,4,7], k &#x3D; 5</span><br><span class="line">Output: 4</span><br><span class="line">Explanation:</span><br><span class="line">The longest subsequence that meets the requirements is [4,5,8,12].</span><br><span class="line">The subsequence has a length of 4, so we return 4.</span><br></pre></td></tr></table></figure>

<p><strong>Example 3:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [1,5], k &#x3D; 1</span><br><span class="line">Output: 1</span><br><span class="line">Explanation:</span><br><span class="line">The longest subsequence that meets the requirements is [1].</span><br><span class="line">The subsequence has a length of 1, so we return 1.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 105</code></li>
<li><code>1 &lt;= nums[i], k &lt;= 105</code></li>
</ul>
<p>Hard，线段树</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line">    <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; max;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">modify</span><span class="params">(<span class="keyword">int</span> o, <span class="keyword">int</span> l, <span class="keyword">int</span> r, <span class="keyword">int</span> i, <span class="keyword">int</span> val)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (l == r) &#123;</span><br><span class="line">            max[o] = val;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> m = (l + r) / <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">if</span> (i &lt;= m) modify(o * <span class="number">2</span>, l, m, i, val);</span><br><span class="line">        <span class="keyword">else</span> modify(o * <span class="number">2</span> + <span class="number">1</span>, m + <span class="number">1</span>, r, i, val);</span><br><span class="line">        max[o] = <span class="built_in">std</span>::max(max[o * <span class="number">2</span>], max[o * <span class="number">2</span> + <span class="number">1</span>]);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 返回区间 [L,R] 内的最大值</span></span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">query</span><span class="params">(<span class="keyword">int</span> o, <span class="keyword">int</span> l, <span class="keyword">int</span> r, <span class="keyword">int</span> L, <span class="keyword">int</span> R)</span> </span>&#123; <span class="comment">// L 和 R 在整个递归过程中均不变，将其大写，视作常量</span></span><br><span class="line">        <span class="keyword">if</span> (L &lt;= l &amp;&amp; r &lt;= R) <span class="keyword">return</span> max[o];</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> m = (l + r) / <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">if</span> (L &lt;= m) res = query(o * <span class="number">2</span>, l, m, L, R);</span><br><span class="line">        <span class="keyword">if</span> (R &gt; m) res = <span class="built_in">std</span>::max(res, query(o * <span class="number">2</span> + <span class="number">1</span>, m + <span class="number">1</span>, r, L, R));</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">lengthOfLIS</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; &amp;nums, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> u = *max_element(nums.begin(), nums.end());</span><br><span class="line">        max.resize(u * <span class="number">4</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> num: nums) &#123;</span><br><span class="line">            <span class="keyword">if</span> (num == <span class="number">1</span>) modify(<span class="number">1</span>, <span class="number">1</span>, u, <span class="number">1</span>, <span class="number">1</span>);</span><br><span class="line">            <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">int</span> res = <span class="number">1</span> + query(<span class="number">1</span>, <span class="number">1</span>, u, <span class="built_in">std</span>::max(num - k, <span class="number">1</span>), num - <span class="number">1</span>);</span><br><span class="line">                modify(<span class="number">1</span>, <span class="number">1</span>, u, num, res);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> max[<span class="number">1</span>];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-311"><a href="#Weekly-Contest-311" class="headerlink" title="Weekly Contest 311"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-311/">Weekly Contest 311</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220919090436.png" alt="image-20220919090430242"></p>
<h3 id="2413-Smallest-Even-Multiple"><a href="#2413-Smallest-Even-Multiple" class="headerlink" title="2413. Smallest Even Multiple"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/smallest-even-multiple/">2413. Smallest Even Multiple</a></h3><p>Given a <strong>positive</strong> integer <code>n</code>, return <em>the smallest positive integer that is a multiple of <strong>both</strong></em> <code>2</code> <em>and</em> <code>n</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: n &#x3D; 5</span><br><span class="line">Output: 10</span><br><span class="line">Explanation: The smallest multiple of both 5 and 2 is 10.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: n &#x3D; 6</span><br><span class="line">Output: 6</span><br><span class="line">Explanation: The smallest multiple of both 6 and 2 is 6. Note that a number is a multiple of itself.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 150</code></li>
</ul>
<p>Easy，查看是否是偶数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">smallestEvenMultiple</span><span class="params">(<span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (n % <span class="number">2</span> == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> n;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">2</span> * n;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2414-Length-of-the-Longest-Alphabetical-Continuous-Substring"><a href="#2414-Length-of-the-Longest-Alphabetical-Continuous-Substring" class="headerlink" title="2414. Length of the Longest Alphabetical Continuous Substring"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/">2414. Length of the Longest Alphabetical Continuous Substring</a></h3><p>An <strong>alphabetical continuous string</strong> is a string consisting of consecutive letters in the alphabet. In other words, it is any substring of the string <code>&quot;abcdefghijklmnopqrstuvwxyz&quot;</code>.</p>
<ul>
<li>For example, <code>&quot;abc&quot;</code> is an alphabetical continuous string, while <code>&quot;acb&quot;</code> and <code>&quot;za&quot;</code> are not.</li>
</ul>
<p>Given a string <code>s</code> consisting of lowercase letters only, return the <em>length of the <strong>longest</strong> alphabetical continuous substring.</em></p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;abacaba&quot;</span><br><span class="line">Output: 2</span><br><span class="line">Explanation: There are 4 distinct continuous substrings: &quot;a&quot;, &quot;b&quot;, &quot;c&quot; and &quot;ab&quot;.</span><br><span class="line">&quot;ab&quot; is the longest continuous substring.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;abcde&quot;</span><br><span class="line">Output: 5</span><br><span class="line">Explanation: &quot;abcde&quot; is the longest continuous substring.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 105</code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
<p>Medium，滑动窗口</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">longestContinuousSubstring</span><span class="params">(<span class="built_in">string</span> s)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">int</span> pre = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> cur = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">while</span> (cur &lt; s.size()) &#123;</span><br><span class="line">            <span class="keyword">while</span> (cur &lt; s.size() &amp;&amp; s[cur] - s[cur - <span class="number">1</span>] == <span class="number">1</span>) &#123;</span><br><span class="line">                cur++;</span><br><span class="line">            &#125;</span><br><span class="line">            res = max(res, cur - pre);</span><br><span class="line">            pre = cur;</span><br><span class="line">            cur++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2415-Reverse-Odd-Levels-of-Binary-Tree"><a href="#2415-Reverse-Odd-Levels-of-Binary-Tree" class="headerlink" title="2415. Reverse Odd Levels of Binary Tree"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/">2415. Reverse Odd Levels of Binary Tree</a></h3><p>Given the <code>root</code> of a <strong>perfect</strong> binary tree, reverse the node values at each <strong>odd</strong> level of the tree.</p>
<ul>
<li>For example, suppose the node values at level 3 are <code>[2,1,3,4,7,11,29,18]</code>, then it should become <code>[18,29,11,7,4,3,1,2]</code>.</li>
</ul>
<p>Return <em>the root of the reversed tree</em>.</p>
<p>A binary tree is <strong>perfect</strong> if all parent nodes have two children and all leaves are on the same level.</p>
<p>The <strong>level</strong> of a node is the number of edges along the path between it and the root node.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220922224147.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: root &#x3D; [2,3,5,8,13,21,34]</span><br><span class="line">Output: [2,5,3,8,13,21,34]</span><br><span class="line">Explanation: </span><br><span class="line">The tree has only one odd level.</span><br><span class="line">The nodes at level 1 are 3, 5 respectively, which are reversed and become 5, 3.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220922224205.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: root &#x3D; [7,13,11]</span><br><span class="line">Output: [7,11,13]</span><br><span class="line">Explanation: </span><br><span class="line">The nodes at level 1 are 13, 11, which are reversed and become 11, 13.</span><br></pre></td></tr></table></figure>

<p><strong>Example 3:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: root &#x3D; [0,1,2,0,0,0,0,1,1,1,1,2,2,2,2]</span><br><span class="line">Output: [0,2,1,0,0,0,0,2,2,2,2,1,1,1,1]</span><br><span class="line">Explanation: </span><br><span class="line">The odd levels have non-zero values.</span><br><span class="line">The nodes at level 1 were 1, 2, and are 2, 1 after the reversal.</span><br><span class="line">The nodes at level 3 were 1, 1, 1, 1, 2, 2, 2, 2, and are 2, 2, 2, 2, 1, 1, 1, 1 after the reversal.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 214]</code>.</li>
<li><code>0 &lt;= Node.val &lt;= 105</code></li>
<li><code>root</code> is a <strong>perfect</strong> binary tree.</li>
</ul>
<p>Medium，BFS 层次遍历</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Definition for a binary tree node.</span></span><br><span class="line"><span class="comment"> * struct TreeNode &#123;</span></span><br><span class="line"><span class="comment"> *     int val;</span></span><br><span class="line"><span class="comment"> *     TreeNode *left;</span></span><br><span class="line"><span class="comment"> *     TreeNode *right;</span></span><br><span class="line"><span class="comment"> *     TreeNode() : val(0), left(nullptr), right(nullptr) &#123;&#125;</span></span><br><span class="line"><span class="comment"> *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) &#123;&#125;</span></span><br><span class="line"><span class="comment"> *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) &#123;&#125;</span></span><br><span class="line"><span class="comment"> * &#125;;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function">TreeNode* <span class="title">reverseOddLevels</span><span class="params">(TreeNode* root)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> level = <span class="number">0</span>;</span><br><span class="line">        <span class="built_in">queue</span>&lt;TreeNode*&gt; q;</span><br><span class="line">        q.push(root);</span><br><span class="line">        <span class="keyword">while</span> (!q.empty()) &#123;</span><br><span class="line">            level++;</span><br><span class="line">            <span class="keyword">int</span> sz = q.size();</span><br><span class="line">            <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">res</span><span class="params">(sz)</span></span>;</span><br><span class="line">            <span class="built_in">queue</span>&lt;TreeNode*&gt; qLevel;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; sz; i++) &#123;</span><br><span class="line">                TreeNode* node = q.front();</span><br><span class="line">                res[i] = node-&gt;val;</span><br><span class="line">                qLevel.push(node);</span><br><span class="line">                q.pop();</span><br><span class="line">                <span class="keyword">if</span> (node-&gt;left) &#123;</span><br><span class="line">                    q.push(node-&gt;left);</span><br><span class="line">                &#125;    </span><br><span class="line">                <span class="keyword">if</span> (node-&gt;right) &#123;</span><br><span class="line">                    q.push(node-&gt;right);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (level % <span class="number">2</span> == <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; sz; i++) &#123;</span><br><span class="line">                    TreeNode* node = qLevel.front();</span><br><span class="line">                    node-&gt;val = res[sz - <span class="number">1</span> - i];</span><br><span class="line">                    qLevel.pop();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> root;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2416-Sum-of-Prefix-Scores-of-Strings"><a href="#Failed-2416-Sum-of-Prefix-Scores-of-Strings" class="headerlink" title="Failed: 2416. Sum of Prefix Scores of Strings"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/sum-of-prefix-scores-of-strings/">2416. Sum of Prefix Scores of Strings</a></h3><p>You are given an array <code>words</code> of size <code>n</code> consisting of <strong>non-empty</strong> strings.</p>
<p>We define the <strong>score</strong> of a string <code>word</code> as the <strong>number</strong> of strings <code>words[i]</code> such that <code>word</code> is a <strong>prefix</strong> of <code>words[i]</code>.</p>
<ul>
<li>For example, if <code>words = [&quot;a&quot;, &quot;ab&quot;, &quot;abc&quot;, &quot;cab&quot;]</code>, then the score of <code>&quot;ab&quot;</code> is <code>2</code>, since <code>&quot;ab&quot;</code> is a prefix of both <code>&quot;ab&quot;</code> and <code>&quot;abc&quot;</code>.</li>
</ul>
<p>Return <em>an array</em> <code>answer</code> <em>of size</em> <code>n</code> <em>where</em> <code>answer[i]</code> <em>is the <strong>sum</strong> of scores of every <strong>non-empty</strong> prefix of</em> <code>words[i]</code>.</p>
<p><strong>Note</strong> that a string is considered as a prefix of itself.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">Input: words &#x3D; [&quot;abc&quot;,&quot;ab&quot;,&quot;bc&quot;,&quot;b&quot;]</span><br><span class="line">Output: [5,4,3,2]</span><br><span class="line">Explanation: The answer for each string is the following:</span><br><span class="line">- &quot;abc&quot; has 3 prefixes: &quot;a&quot;, &quot;ab&quot;, and &quot;abc&quot;.</span><br><span class="line">- There are 2 strings with the prefix &quot;a&quot;, 2 strings with the prefix &quot;ab&quot;, and 1 string with the prefix &quot;abc&quot;.</span><br><span class="line">The total is answer[0] &#x3D; 2 + 2 + 1 &#x3D; 5.</span><br><span class="line">- &quot;ab&quot; has 2 prefixes: &quot;a&quot; and &quot;ab&quot;.</span><br><span class="line">- There are 2 strings with the prefix &quot;a&quot;, and 2 strings with the prefix &quot;ab&quot;.</span><br><span class="line">The total is answer[1] &#x3D; 2 + 2 &#x3D; 4.</span><br><span class="line">- &quot;bc&quot; has 2 prefixes: &quot;b&quot; and &quot;bc&quot;.</span><br><span class="line">- There are 2 strings with the prefix &quot;b&quot;, and 1 string with the prefix &quot;bc&quot;.</span><br><span class="line">The total is answer[2] &#x3D; 2 + 1 &#x3D; 3.</span><br><span class="line">- &quot;b&quot; has 1 prefix: &quot;b&quot;.</span><br><span class="line">- There are 2 strings with the prefix &quot;b&quot;.</span><br><span class="line">The total is answer[3] &#x3D; 2.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: words &#x3D; [&quot;abcd&quot;]</span><br><span class="line">Output: [4]</span><br><span class="line">Explanation:</span><br><span class="line">&quot;abcd&quot; has 4 prefixes: &quot;a&quot;, &quot;ab&quot;, &quot;abc&quot;, and &quot;abcd&quot;.</span><br><span class="line">Each prefix has a score of one, so the total is answer[0] &#x3D; 1 + 1 + 1 + 1 &#x3D; 4.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= words.length &lt;= 1000</code></li>
<li><code>1 &lt;= words[i].length &lt;= 1000</code></li>
<li><code>words[i]</code> consists of lowercase English letters.</li>
</ul>
<p>Hard，前缀树</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Trie</span> &#123;</span></span><br><span class="line">    <span class="keyword">public</span>:</span><br><span class="line">        Trie *ch[<span class="number">26</span>] = &#123;&#125;;</span><br><span class="line">        <span class="keyword">int</span> visited = <span class="number">0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">sumPrefixScores</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt;&amp; words)</span> </span>&#123;</span><br><span class="line">        Trie trie;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; ans;</span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">string</span>&amp; word: words) &#123;</span><br><span class="line">            <span class="keyword">auto</span> t = &amp;trie;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">char</span>&amp; c: word) &#123;</span><br><span class="line">                <span class="keyword">if</span> (!t-&gt;ch[c - <span class="string">&#x27;a&#x27;</span>]) t-&gt;ch[c - <span class="string">&#x27;a&#x27;</span>] = <span class="keyword">new</span> Trie();</span><br><span class="line">                t-&gt;ch[c - <span class="string">&#x27;a&#x27;</span>]-&gt;visited++;</span><br><span class="line">                t = t-&gt;ch[c - <span class="string">&#x27;a&#x27;</span>];</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">string</span>&amp; word: words) &#123;</span><br><span class="line">            <span class="keyword">auto</span> t = &amp;trie;</span><br><span class="line">            <span class="keyword">int</span> cur = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">char</span>&amp; c: word) &#123;</span><br><span class="line">                cur += t-&gt;ch[c - <span class="string">&#x27;a&#x27;</span>]-&gt;visited;</span><br><span class="line">                t = t-&gt;ch[c - <span class="string">&#x27;a&#x27;</span>];</span><br><span class="line">            &#125;</span><br><span class="line">            ans.push_back(cur);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-312"><a href="#Weekly-Contest-312" class="headerlink" title="Weekly Contest 312"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-312/">Weekly Contest 312</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220925135205.png" alt="image-20220925135159723"></p>
<h3 id="2418-Sort-the-People"><a href="#2418-Sort-the-People" class="headerlink" title="2418. Sort the People"></a>2418. Sort the People</h3><p>You are given an array of strings <code>names</code>, and an array <code>heights</code> that consists of <strong>distinct</strong> positive integers. Both arrays are of length <code>n</code>.</p>
<p>For each index <code>i</code>, <code>names[i]</code> and <code>heights[i]</code> denote the name and height of the <code>ith</code> person.</p>
<p>Return <code>names</code> <em>sorted in <strong>descending</strong> order by the people’s heights</em>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: names &#x3D; [&quot;Mary&quot;,&quot;John&quot;,&quot;Emma&quot;], heights &#x3D; [180,165,170]</span><br><span class="line">Output: [&quot;Mary&quot;,&quot;Emma&quot;,&quot;John&quot;]</span><br><span class="line">Explanation: Mary is the tallest, followed by Emma and John.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: names &#x3D; [&quot;Alice&quot;,&quot;Bob&quot;,&quot;Bob&quot;], heights &#x3D; [155,185,150]</span><br><span class="line">Output: [&quot;Bob&quot;,&quot;Alice&quot;,&quot;Bob&quot;]</span><br><span class="line">Explanation: The first Bob is the tallest, followed by Alice and the second Bob.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == names.length == heights.length</code></li>
<li><code>1 &lt;= n &lt;= 103</code></li>
<li><code>1 &lt;= names[i].length &lt;= 20</code></li>
<li><code>1 &lt;= heights[i] &lt;= 105</code></li>
<li><code>names[i]</code> consists of lower and upper case English letters.</li>
<li>All the values of <code>heights</code> are distinct.</li>
</ul>
<p>Easy，排序</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt; <span class="title">sortPeople</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt;&amp; names, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; heights)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">pair</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt;&gt; heightS;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; heights.size(); i++) &#123;</span><br><span class="line">            heightS.push_back(&#123;heights[i], i&#125;);</span><br><span class="line">        &#125;</span><br><span class="line">        sort(heightS.begin(), heightS.end());</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt; res;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = heights.size() - <span class="number">1</span>; i &gt;= <span class="number">0</span>; i--) &#123;</span><br><span class="line">            res.push_back(names[heightS[i].second]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>或者参考别人的</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Person</span> &#123;</span></span><br><span class="line">    <span class="built_in">string</span> name;</span><br><span class="line">    <span class="keyword">int</span> height;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span>    </span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt; <span class="title">sortPeople</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt;&amp; names, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; heights)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">vector</span>&lt;Person&gt; people;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; names.size(); i++) &#123;</span><br><span class="line">            Person p&#123;names[i], heights[i]&#125;;</span><br><span class="line">            people.push_back(p);</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="built_in">std</span>::sort(people.begin(), people.end(), [](<span class="keyword">const</span> Person &amp; a, <span class="keyword">const</span> Person &amp; b) -&gt; <span class="keyword">bool</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> b.height &lt; a.height + <span class="number">1</span>;</span><br><span class="line">        &#125;);</span><br><span class="line">        </span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt; res;</span><br><span class="line">        <span class="keyword">for</span> (Person p : people) &#123;</span><br><span class="line">            res.push_back(p.name);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2419-Longest-Subarray-With-Maximum-Bitwise-AND"><a href="#2419-Longest-Subarray-With-Maximum-Bitwise-AND" class="headerlink" title="2419. Longest Subarray With Maximum Bitwise AND"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/longest-subarray-with-maximum-bitwise-and/">2419. Longest Subarray With Maximum Bitwise AND</a></h3><p>You are given an integer array <code>nums</code> of size <code>n</code>.</p>
<p>Consider a <strong>non-empty</strong> subarray from <code>nums</code> that has the <strong>maximum</strong> possible <strong>bitwise AND</strong>.</p>
<ul>
<li>In other words, let <code>k</code> be the maximum value of the bitwise AND of <strong>any</strong> subarray of <code>nums</code>. Then, only subarrays with a bitwise AND equal to <code>k</code> should be considered.</li>
</ul>
<p>Return <em>the length of the <strong>longest</strong> such subarray</em>.</p>
<p>The bitwise AND of an array is the bitwise AND of all the numbers in it.</p>
<p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [1,2,3,3,2,2]</span><br><span class="line">Output: 2</span><br><span class="line">Explanation:</span><br><span class="line">The maximum possible bitwise AND of a subarray is 3.</span><br><span class="line">The longest subarray with that value is [3,3], so we return 2.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [1,2,3,4]</span><br><span class="line">Output: 1</span><br><span class="line">Explanation:</span><br><span class="line">The maximum possible bitwise AND of a subarray is 4.</span><br><span class="line">The longest subarray with that value is [4], so we return 1.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 105</code></li>
<li><code>1 &lt;= nums[i] &lt;= 106</code></li>
</ul>
<p>Medium，翻译一下就是最大数的最长长度</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">longestSubarray</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> maxVal = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> maxLen = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; nums.size(); ) &#123;</span><br><span class="line">            <span class="keyword">if</span> (nums[i] &gt;= maxVal) &#123;</span><br><span class="line">                <span class="keyword">int</span> j = i + <span class="number">1</span>;</span><br><span class="line">                <span class="keyword">while</span> (j &lt; nums.size() &amp;&amp; nums[j] == nums[i]) &#123;</span><br><span class="line">                    j++;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (maxVal == nums[i]) &#123;</span><br><span class="line">                    maxLen = max(maxLen, j - i);</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    maxLen = j - i;</span><br><span class="line">                &#125;</span><br><span class="line">                maxVal = nums[i];</span><br><span class="line">                i = j;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                i++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> maxLen;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2420-Find-All-Good-Indices"><a href="#2420-Find-All-Good-Indices" class="headerlink" title="2420. Find All Good Indices"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/find-all-good-indices/">2420. Find All Good Indices</a></h3><p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code> and a positive integer <code>k</code>.</p>
<p>We call an index <code>i</code> in the range <code>k &lt;= i &lt; n - k</code> <strong>good</strong> if the following conditions are satisfied:</p>
<ul>
<li>The <code>k</code> elements that are just <strong>before</strong> the index <code>i</code> are in <strong>non-increasing</strong> order.</li>
<li>The <code>k</code> elements that are just <strong>after</strong> the index <code>i</code> are in <strong>non-decreasing</strong> order.</li>
</ul>
<p>Return <em>an array of all good indices sorted in <strong>increasing</strong> order</em>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [2,1,1,1,3,4,1], k &#x3D; 2</span><br><span class="line">Output: [2,3]</span><br><span class="line">Explanation: There are two good indices in the array:</span><br><span class="line">- Index 2. The subarray [2,1] is in non-increasing order, and the subarray [1,3] is in non-decreasing order.</span><br><span class="line">- Index 3. The subarray [1,1] is in non-increasing order, and the subarray [3,4] is in non-decreasing order.</span><br><span class="line">Note that the index 4 is not good because [4,1] is not non-decreasing.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [2,1,1,2], k &#x3D; 2</span><br><span class="line">Output: []</span><br><span class="line">Explanation: There are no good indices in this array.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>3 &lt;= n &lt;= 105</code></li>
<li><code>1 &lt;= nums[i] &lt;= 106</code></li>
<li><code>1 &lt;= k &lt;= n / 2</code></li>
</ul>
<p>Medium，使用动态规划计算当前元素结尾递增/递减数组长度，</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">goodIndices</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = nums.size();</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">dec</span><span class="params">(n, <span class="number">1</span>)</span></span>;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">inc</span><span class="params">(n, <span class="number">1</span>)</span></span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (nums[i] &gt; nums[i - <span class="number">1</span>]) &#123;</span><br><span class="line">                inc[i] = inc[i - <span class="number">1</span>] + <span class="number">1</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (nums[i] &lt; nums[i - <span class="number">1</span>]) &#123;</span><br><span class="line">                dec[i] = dec[i - <span class="number">1</span>] + <span class="number">1</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                inc[i] = inc[i - <span class="number">1</span>] + <span class="number">1</span>;</span><br><span class="line">                dec[i] = dec[i - <span class="number">1</span>] + <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; res;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = k; i + k &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (dec[i - <span class="number">1</span>] &gt;= k &amp;&amp; inc[i + k] &gt;= k ) &#123;</span><br><span class="line">                res.push_back(i);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2421-Number-of-Good-Paths"><a href="#Failed-2421-Number-of-Good-Paths" class="headerlink" title="Failed: 2421. Number of Good Paths"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/number-of-good-paths/">2421. Number of Good Paths</a></h3><p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>ith</code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [ai, bi]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>ai</code> and <code>bi</code>.</p>
<p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p>
<ol>
<li>The starting node and the ending node have the <strong>same</strong> value.</li>
<li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node’s value should be the maximum value along the path).</li>
</ol>
<p>Return <em>the number of distinct good paths</em>.</p>
<p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220925142712.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: vals &#x3D; [1,3,2,1,3], edges &#x3D; [[0,1],[0,2],[2,3],[2,4]]</span><br><span class="line">Output: 6</span><br><span class="line">Explanation: There are 5 good paths consisting of a single node.</span><br><span class="line">There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4.</span><br><span class="line">(The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.)</span><br><span class="line">Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0].</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220925142741.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: vals &#x3D; [1,1,2,2,3], edges &#x3D; [[0,1],[1,2],[2,3],[2,4]]</span><br><span class="line">Output: 7</span><br><span class="line">Explanation: There are 5 good paths consisting of a single node.</span><br><span class="line">There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3.</span><br></pre></td></tr></table></figure>

<p><strong>Example 3:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220925223608.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: vals &#x3D; [1], edges &#x3D; []</span><br><span class="line">Output: 1</span><br><span class="line">Explanation: The tree consists of only one node, so there is one good path.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == vals.length</code></li>
<li><code>1 &lt;= n &lt;= 3 * 104</code></li>
<li><code>0 &lt;= vals[i] &lt;= 105</code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 &lt;= ai, bi &lt; n</code></li>
<li><code>ai != bi</code></li>
<li><code>edges</code> represents a valid tree.</li>
</ul>
<p>Hard，并查集</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">numberOfGoodPaths</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; vals, <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; edges)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = vals.size();</span><br><span class="line">        <span class="comment">// 建图</span></span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; graph(n);</span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; edge: edges) &#123;</span><br><span class="line">            graph[edge[<span class="number">0</span>]].push_back(edge[<span class="number">1</span>]);</span><br><span class="line">            graph[edge[<span class="number">1</span>]].push_back(edge[<span class="number">0</span>]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 并查集模板</span></span><br><span class="line">        <span class="comment">// size[x] 表示节点值等于 vals[x] 的节点个数，如果按照节点值从小到大合并，size[x] 也是连通块内的等于最大节点值的节点个数</span></span><br><span class="line">        </span><br><span class="line">        <span class="keyword">int</span> id[n], fa[n], size[n]; <span class="comment">// id 后面排序用</span></span><br><span class="line">        iota(id, id + n, <span class="number">0</span>);</span><br><span class="line">        iota(fa, fa + n, <span class="number">0</span>);</span><br><span class="line">        fill(size, size + n, <span class="number">1</span>);</span><br><span class="line">        function&lt;<span class="keyword">int</span>(<span class="keyword">int</span>)&gt; find = [&amp;](<span class="keyword">int</span> x) -&gt; <span class="keyword">int</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> fa[x] == x ? x : fa[x] = find(fa[x]);</span><br><span class="line">        &#125;;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">int</span> ans = n;</span><br><span class="line">        sort(id, id + n, [&amp;](<span class="keyword">int</span> i, <span class="keyword">int</span> j) &#123;</span><br><span class="line">            <span class="keyword">return</span> vals[i] &lt; vals[j];</span><br><span class="line">        &#125;);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> x : id) &#123;</span><br><span class="line">            <span class="keyword">int</span> vx = vals[x], fx = find(x);</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> y : graph[x]) &#123;</span><br><span class="line">                y = find(y);</span><br><span class="line">                <span class="comment">// 只考虑最大节点值比 vx 小的连通块</span></span><br><span class="line">                <span class="keyword">if</span> (y == fx || vals[y] &gt; vx) <span class="keyword">continue</span>;</span><br><span class="line">                <span class="comment">// 可以构成好路径</span></span><br><span class="line">                <span class="keyword">if</span> (vals[y] == vx) &#123;</span><br><span class="line">                    <span class="comment">// 乘法原理</span></span><br><span class="line">                    ans += size[fx] * size[y];</span><br><span class="line">                    <span class="comment">// 统计连通块内节点值等于 vx 的节点个数</span></span><br><span class="line">                    size[fx] += size[y];</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">// 把小的节点值合并到大的节点值上</span></span><br><span class="line">                fa[y] = fx;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-312-1"><a href="#Weekly-Contest-312-1" class="headerlink" title="Weekly Contest 312"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-312/">Weekly Contest 312</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20221002154624.png" alt="image-20221002154624485"></p>
<h3 id="2427-Number-of-Common-Factors"><a href="#2427-Number-of-Common-Factors" class="headerlink" title="2427. Number of Common Factors"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/number-of-common-factors/">2427. Number of Common Factors</a></h3><p>Given two positive integers <code>a</code> and <code>b</code>, return <em>the number of <strong>common</strong> factors of</em> <code>a</code> <em>and</em> <code>b</code>.</p>
<p>An integer <code>x</code> is a <strong>common factor</strong> of <code>a</code> and <code>b</code> if <code>x</code> divides both <code>a</code> and <code>b</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: a &#x3D; 12, b &#x3D; 6</span><br><span class="line">Output: 4</span><br><span class="line">Explanation: The common factors of 12 and 6 are 1, 2, 3, 6.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: a &#x3D; 25, b &#x3D; 30</span><br><span class="line">Output: 2</span><br><span class="line">Explanation: The common factors of 25 and 30 are 1, 5.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= a, b &lt;= 1000</code></li>
</ul>
<p>Easy，最大公约数，应有更好的方法，找到后分解质因数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">commonFactors</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> num = <span class="built_in">std</span>::gcd(a, b);</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">2</span>; i &lt;= num; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (num % i == <span class="number">0</span>) res++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2428-Maximum-Sum-of-an-Hourglass"><a href="#2428-Maximum-Sum-of-an-Hourglass" class="headerlink" title="2428. Maximum Sum of an Hourglass"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/maximum-sum-of-an-hourglass/">2428. Maximum Sum of an Hourglass</a></h3><p>You are given an <code>m x n</code> integer matrix <code>grid</code>.</p>
<p>We define an <strong>hourglass</strong> as a part of the matrix with the following form:</p>
<p><img data-src="https://assets.leetcode.com/uploads/2022/08/21/img.jpg" alt="img"></p>
<p>Return <em>the <strong>maximum</strong> sum of the elements of an hourglass</em>.</p>
<p><strong>Note</strong> that an hourglass cannot be rotated and must be entirely contained within the matrix.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://assets.leetcode.com/uploads/2022/08/21/1.jpg" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: grid &#x3D; [[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]]</span><br><span class="line">Output: 30</span><br><span class="line">Explanation: The cells shown above represent the hourglass with the maximum sum: 6 + 2 + 1 + 2 + 9 + 2 + 8 &#x3D; 30.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://assets.leetcode.com/uploads/2022/08/21/2.jpg" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: grid &#x3D; [[1,2,3],[4,5,6],[7,8,9]]</span><br><span class="line">Output: 35</span><br><span class="line">Explanation: There is only one hourglass in the matrix, with the sum: 1 + 2 + 3 + 5 + 7 + 8 + 9 &#x3D; 35.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>3 &lt;= m, n &lt;= 150</code></li>
<li><code>0 &lt;= grid[i][j] &lt;= 106</code></li>
</ul>
<p>Medium，直接计算每个窗口之和，理论上可以使用滑动窗口优化</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">maxSum</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; grid)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> m = grid.size(), n = grid[<span class="number">0</span>].size();</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; m - <span class="number">2</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; n - <span class="number">2</span>; j++) &#123;</span><br><span class="line">                <span class="keyword">int</span> num = grid[i][j] + grid[i][j + <span class="number">1</span>] + grid[i][j + <span class="number">2</span>] + grid[i + <span class="number">1</span>][j + <span class="number">1</span>] + grid[i + <span class="number">2</span>][j] + grid[i + <span class="number">2</span>][j + <span class="number">1</span>] + grid[i + <span class="number">2</span>][j + <span class="number">2</span>];</span><br><span class="line">                <span class="keyword">if</span> (num &gt; res) res = num;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2429-Minimize-XOR"><a href="#2429-Minimize-XOR" class="headerlink" title="2429. Minimize XOR"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/minimize-xor/">2429. Minimize XOR</a></h3><p>Given two positive integers <code>num1</code> and <code>num2</code>, find the integer <code>x</code> such that:</p>
<ul>
<li><code>x</code> has the same number of set bits as <code>num2</code>, and</li>
<li>The value <code>x XOR num1</code> is <strong>minimal</strong>.</li>
</ul>
<p>Note that <code>XOR</code> is the bitwise XOR operation.</p>
<p>Return <em>the integer</em> <code>x</code>. The test cases are generated such that <code>x</code> is <strong>uniquely determined</strong>.</p>
<p>The number of <strong>set bits</strong> of an integer is the number of <code>1</code>‘s in its binary representation.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: num1 &#x3D; 3, num2 &#x3D; 5</span><br><span class="line">Output: 3</span><br><span class="line">Explanation:</span><br><span class="line">The binary representations of num1 and num2 are 0011 and 0101, respectively.</span><br><span class="line">The integer 3 has the same number of set bits as num2, and the value 3 XOR 3 &#x3D; 0 is minimal.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: num1 &#x3D; 1, num2 &#x3D; 12</span><br><span class="line">Output: 3</span><br><span class="line">Explanation:</span><br><span class="line">The binary representations of num1 and num2 are 0001 and 1100, respectively.</span><br><span class="line">The integer 3 has the same number of set bits as num2, and the value 3 XOR 1 &#x3D; 2 is minimal.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= num1, num2 &lt;= 109</code></li>
</ul>
<p>Medium，位计算</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">minimizeXor</span><span class="params">(<span class="keyword">int</span> num1, <span class="keyword">int</span> num2)</span> </span>&#123;</span><br><span class="line">        vector&lt;int&gt; bin1(31, 0), bin2(31, 0);</span><br><span class="line">        getBin(num1, bin1);</span><br><span class="line">        getBin(num2, bin2);</span><br><span class="line">        <span class="keyword">int</span> count1 = <span class="built_in">std</span>::accumulate(bin1.begin(), bin1.end(), <span class="number">0</span>);</span><br><span class="line">        <span class="keyword">int</span> count2 = <span class="built_in">std</span>::accumulate(bin2.begin(), bin2.end(), <span class="number">0</span>);</span><br><span class="line">        <span class="keyword">if</span> (count1 == count2) <span class="keyword">return</span> num1;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; bin = bin1;</span><br><span class="line">        <span class="keyword">if</span> (count1 &lt; count2) &#123;</span><br><span class="line">            <span class="keyword">int</span> diff = count2 - count1;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">31</span>; i++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (bin1[i] == <span class="number">0</span>) &#123;</span><br><span class="line">                    bin[i] = <span class="number">1</span>;</span><br><span class="line">                    diff--;</span><br><span class="line">                    <span class="keyword">if</span> (diff == <span class="number">0</span>) &#123;</span><br><span class="line">                        <span class="keyword">break</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">int</span> diff = count1 - count2;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">31</span>; i++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (bin1[i] == <span class="number">1</span>) &#123;</span><br><span class="line">                    bin[i] = <span class="number">0</span>;</span><br><span class="line">                    diff--;</span><br><span class="line">                    <span class="keyword">if</span> (diff == <span class="number">0</span>) &#123;</span><br><span class="line">                        <span class="keyword">break</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> genNum(bin);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">getBin</span><span class="params">(<span class="keyword">int</span> num, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; &amp;bin)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> count = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span> (num != <span class="number">0</span>) &#123;</span><br><span class="line">            bin[count] = num &amp; <span class="number">1</span>;</span><br><span class="line">            count++;</span><br><span class="line">            num = num &gt;&gt; <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">genNum</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; &amp;bin)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">0</span>, cur = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">31</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (bin[i] == <span class="number">1</span>) &#123;</span><br><span class="line">                res = res | cur;</span><br><span class="line">            &#125;</span><br><span class="line">            cur = cur &lt;&lt; <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>同样的逻辑，看看人大佬写得多优雅</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">minimizeXor</span><span class="params">(<span class="keyword">int</span> num1, <span class="keyword">int</span> num2)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> a = __builtin_popcount(num1), b = __builtin_popcount(num2), res = num1;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">32</span>; ++i) &#123;</span><br><span class="line">            <span class="keyword">if</span> (a &gt; b &amp;&amp; ((<span class="number">1</span> &lt;&lt; i) &amp; num1) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                res ^= <span class="number">1</span> &lt;&lt; i;</span><br><span class="line">                a--;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (a &lt; b &amp;&amp; ((<span class="number">1</span> &lt;&lt; i) &amp; num1) == <span class="number">0</span>) &#123;</span><br><span class="line">                res ^= <span class="number">1</span> &lt;&lt; i;</span><br><span class="line">                a++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2430-Maximum-Deletions-on-a-String"><a href="#2430-Maximum-Deletions-on-a-String" class="headerlink" title="2430. Maximum Deletions on a String"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/maximum-deletions-on-a-string/">2430. Maximum Deletions on a String</a></h3><p>You are given a string <code>s</code> consisting of only lowercase English letters. In one operation, you can:</p>
<ul>
<li>Delete <strong>the entire string</strong> <code>s</code>, or</li>
<li>Delete the <strong>first</strong> <code>i</code> letters of <code>s</code> if the first <code>i</code> letters of <code>s</code> are <strong>equal</strong> to the following <code>i</code> letters in <code>s</code>, for any <code>i</code> in the range <code>1 &lt;= i &lt;= s.length / 2</code>.</li>
</ul>
<p>For example, if <code>s = &quot;ababc&quot;</code>, then in one operation, you could delete the first two letters of <code>s</code> to get <code>&quot;abc&quot;</code>, since the first two letters of <code>s</code> and the following two letters of <code>s</code> are both equal to <code>&quot;ab&quot;</code>.</p>
<p>Return <em>the <strong>maximum</strong> number of operations needed to delete all of</em> <code>s</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;abcabcdabc&quot;</span><br><span class="line">Output: 2</span><br><span class="line">Explanation:</span><br><span class="line">- Delete the first 3 letters (&quot;abc&quot;) since the next 3 letters are equal. Now, s &#x3D; &quot;abcdabc&quot;.</span><br><span class="line">- Delete all the letters.</span><br><span class="line">We used 2 operations so return 2. It can be proven that 2 is the maximum number of operations needed.</span><br><span class="line">Note that in the second operation we cannot delete &quot;abc&quot; again because the next occurrence of &quot;abc&quot; does not happen in the next 3 letters.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;aaabaab&quot;</span><br><span class="line">Output: 4</span><br><span class="line">Explanation:</span><br><span class="line">- Delete the first letter (&quot;a&quot;) since the next letter is equal. Now, s &#x3D; &quot;aabaab&quot;.</span><br><span class="line">- Delete the first 3 letters (&quot;aab&quot;) since the next 3 letters are equal. Now, s &#x3D; &quot;aab&quot;.</span><br><span class="line">- Delete the first letter (&quot;a&quot;) since the next letter is equal. Now, s &#x3D; &quot;ab&quot;.</span><br><span class="line">- Delete all the letters.</span><br><span class="line">We used 4 operations so return 4. It can be proven that 4 is the maximum number of operations needed.</span><br></pre></td></tr></table></figure>

<p><strong>Example 3:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;aaaaa&quot;</span><br><span class="line">Output: 5</span><br><span class="line">Explanation: In each operation, we can delete the first letter of s.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 4000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
<p>Hard，ts暴力可解</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">dup</span>(<span class="params">s: <span class="built_in">string</span></span>): <span class="title">boolean</span> </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> len = s.length;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">1</span>; i &lt;= <span class="built_in">Math</span>.floor(len / <span class="number">2</span>); i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (len % i == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">let</span> t = s.slice(<span class="number">0</span>, i);</span><br><span class="line">            <span class="keyword">let</span> j = len / i;</span><br><span class="line">            <span class="keyword">if</span> (s === t.repeat(j)) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">deleteString</span>(<span class="params">s: <span class="built_in">string</span></span>): <span class="title">number</span> </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> res: <span class="built_in">number</span> = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span> (s.length &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> len = s.length;</span><br><span class="line">        <span class="keyword">if</span> (dup(s)) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">1</span>; i &lt;= <span class="built_in">Math</span>.floor(len / <span class="number">2</span>); i++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (len % i == <span class="number">0</span>) &#123;</span><br><span class="line">                    <span class="keyword">let</span> t = s.slice(<span class="number">0</span>, i);</span><br><span class="line">                    <span class="keyword">let</span> j = len / i;</span><br><span class="line">                    <span class="keyword">if</span> (s === t.repeat(j)) &#123;</span><br><span class="line">                        s = t;</span><br><span class="line">                        res += j - <span class="number">1</span>;</span><br><span class="line">                        <span class="keyword">break</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">let</span> i = <span class="built_in">Math</span>.floor(len / <span class="number">2</span>);</span><br><span class="line">            <span class="keyword">for</span> (; i &gt;= <span class="number">1</span>; i--) &#123;</span><br><span class="line">                <span class="keyword">const</span> t = s.slice(<span class="number">0</span>, i);</span><br><span class="line">                <span class="keyword">if</span> (s.startsWith(t + t) &amp;&amp; !dup(t)) &#123;</span><br><span class="line">                    s = s.slice(i);</span><br><span class="line">                    res++;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (i == <span class="number">0</span>) &#123;</span><br><span class="line">                res++;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> res;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>C++动态规划</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">isValid</span><span class="params">(<span class="keyword">int</span> first, <span class="keyword">int</span> second, <span class="built_in">string</span> &amp;s)</span></span>&#123;</span><br><span class="line">        <span class="keyword">int</span> i = first;</span><br><span class="line">        <span class="keyword">int</span> j = second;</span><br><span class="line">        <span class="keyword">int</span> n = s.length();</span><br><span class="line">        <span class="keyword">while</span> (i &lt; second) &#123;</span><br><span class="line">            <span class="keyword">if</span> (s[i] == s[j]) &#123;</span><br><span class="line">                i++;</span><br><span class="line">                j++;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (j == n &amp;&amp; i &lt; second)</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">deleteString</span><span class="params">(<span class="built_in">string</span> s)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = s.length();</span><br><span class="line">        <span class="keyword">int</span> dp[n];</span><br><span class="line">        dp[n - <span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line">        <span class="built_in">unordered_map</span>&lt;<span class="keyword">char</span>, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; temp;</span><br><span class="line">        temp[s[n - <span class="number">1</span>]] = &#123;n - <span class="number">1</span>&#125;;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = n - <span class="number">2</span>; i &gt;= <span class="number">0</span>; i--) &#123;</span><br><span class="line">            <span class="keyword">if</span> (temp.find(s[i]) == temp.end()) &#123;</span><br><span class="line">                dp[i] = <span class="number">1</span>;</span><br><span class="line">                temp[s[i]] = &#123;i&#125;;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; st = temp[s[i]];</span><br><span class="line">                <span class="keyword">int</span> maximum = <span class="number">0</span>;</span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> j = st.size() - <span class="number">1</span>; j &gt;= <span class="number">0</span>; j--) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (dp[st[j]] &gt; maximum &amp;&amp; isValid(i, st[j], s))</span><br><span class="line">                        maximum = dp[st[j]];</span><br><span class="line">                &#125;</span><br><span class="line">                dp[i] = maximum + <span class="number">1</span>;</span><br><span class="line">                temp[s[i]].push_back(i);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> dp[<span class="number">0</span>];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    
      
<div>
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
</div>
        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E5%88%B7%E9%A2%98/" rel="tag"><i class="fa fa-tag"></i> 刷题</a>
              <a href="/tags/%E5%8A%9B%E6%89%A3/" rel="tag"><i class="fa fa-tag"></i> 力扣</a>
              <a href="/tags/%E5%91%A8%E8%B5%9B/" rel="tag"><i class="fa fa-tag"></i> 周赛</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2022/06/17/bigfrontend%E7%9A%84React%20problems/" rel="prev" title="bigfrontend的React problems">
      <i class="fa fa-chevron-left"></i> bigfrontend的React problems
    </a></div>
      <div class="post-nav-item">
    <a href="/2022/08/07/%E5%A4%8F%E5%A4%A9%E5%86%B0%E9%9B%AA%E5%B8%A6%E6%9D%A5%E7%9A%84%E7%B2%BE%E7%A5%9E%E5%86%85%E8%80%97/" rel="next" title="夏天冰雪带来的精神内耗">
      夏天冰雪带来的精神内耗 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-301"><span class="nav-text">Weekly Contest 301</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2335-Minimum-Amount-of-Time-to-Fill-Cups"><span class="nav-text">2335. Minimum Amount of Time to Fill Cups</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2336-Smallest-Number-in-Infinite-Set"><span class="nav-text">2336. Smallest Number in Infinite Set</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2337-Move-Pieces-to-Obtain-a-String"><span class="nav-text">2337. Move Pieces to Obtain a String</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2338-Count-the-Number-of-Ideal-Arrays"><span class="nav-text">Failed: 2338. Count the Number of Ideal Arrays</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-302"><span class="nav-text">Weekly Contest 302</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2341-Maximum-Number-of-Pairs-in-Array"><span class="nav-text">2341. Maximum Number of Pairs in Array</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2342-Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits"><span class="nav-text">2342. Max Sum of a Pair With Equal Sum of Digits</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2343-Query-Kth-Smallest-Trimmed-Number"><span class="nav-text">2343. Query Kth Smallest Trimmed Number</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2344-Minimum-Deletions-to-Make-Array-Divisible"><span class="nav-text">2344. Minimum Deletions to Make Array Divisible</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-303"><span class="nav-text">Weekly Contest 303</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2351-First-Letter-to-Appear-Twice"><span class="nav-text">2351. First Letter to Appear Twice</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2352-Equal-Row-and-Column-Pairs"><span class="nav-text">2352. Equal Row and Column Pairs</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2353-Design-a-Food-Rating-System"><span class="nav-text">Failed: 2353. Design a Food Rating System</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2354-Number-of-Excellent-Pairs"><span class="nav-text">Failed: 2354. Number of Excellent Pairs</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-304"><span class="nav-text">Weekly Contest 304</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2357-Make-Array-Zero-by-Subtracting-Equal-Amounts"><span class="nav-text">2357. Make Array Zero by Subtracting Equal Amounts</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2358-Maximum-Number-of-Groups-Entering-a-Competition"><span class="nav-text">Failed: 2358. Maximum Number of Groups Entering a Competition</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2359-Find-Closest-Node-to-Given-Two-Nodes"><span class="nav-text">2359. Find Closest Node to Given Two Nodes</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2360-Longest-Cycle-in-a-Graph"><span class="nav-text">2360. Longest Cycle in a Graph</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-305"><span class="nav-text">Weekly Contest 305</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2367-Number-of-Arithmetic-Triplets"><span class="nav-text">2367. Number of Arithmetic Triplets</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2368-Reachable-Nodes-With-Restrictions"><span class="nav-text">2368. Reachable Nodes With Restrictions</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2369-Check-if-There-is-a-Valid-Partition-For-The-Array"><span class="nav-text">Failed: 2369. Check if There is a Valid Partition For The Array</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2370-Longest-Ideal-Subsequence"><span class="nav-text">2370. Longest Ideal Subsequence</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-306"><span class="nav-text">Weekly Contest 306</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2373-Largest-Local-Values-in-a-Matrix"><span class="nav-text">2373. Largest Local Values in a Matrix</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2374-Node-With-Highest-Edge-Score"><span class="nav-text">2374. Node With Highest Edge Score</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2375-Construct-Smallest-Number-From-DI-String"><span class="nav-text">Failed: 2375. Construct Smallest Number From DI String</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2376-Count-Special-Integers"><span class="nav-text">Failed: 2376. Count Special Integers</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-307"><span class="nav-text">Weekly Contest 307</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2383-Minimum-Hours-of-Training-to-Win-a-Competition"><span class="nav-text">2383. Minimum Hours of Training to Win a Competition</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2384-Largest-Palindromic-Number"><span class="nav-text">2384. Largest Palindromic Number</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2385-Amount-of-Time-for-Binary-Tree-to-Be-Infected"><span class="nav-text">2385. Amount of Time for Binary Tree to Be Infected</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2386-Find-the-K-Sum-of-an-Array"><span class="nav-text">Failed: 2386. Find the K-Sum of an Array</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-308"><span class="nav-text">Weekly Contest 308</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2389-Longest-Subsequence-With-Limited-Sum"><span class="nav-text">2389. Longest Subsequence With Limited Sum</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2390-Removing-Stars-From-a-String"><span class="nav-text">2390. Removing Stars From a String</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2391-Minimum-Amount-of-Time-to-Collect-Garbage"><span class="nav-text">2391. Minimum Amount of Time to Collect Garbage</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2392-Build-a-Matrix-With-Conditions"><span class="nav-text">Failed: 2392. Build a Matrix With Conditions</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-309"><span class="nav-text">Weekly Contest 309</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2399-Check-Distances-Between-Same-Letters"><span class="nav-text">2399. Check Distances Between Same Letters</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2400-Number-of-Ways-to-Reach-a-Position-After-Exactly-k-Steps"><span class="nav-text">Failed: 2400. Number of Ways to Reach a Position After Exactly k Steps</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2401-Longest-Nice-Subarray"><span class="nav-text">Failed: 2401. Longest Nice Subarray</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2402-Meeting-Rooms-III"><span class="nav-text">2402. Meeting Rooms III</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-310"><span class="nav-text">Weekly Contest 310</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2404-Most-Frequent-Even-Element"><span class="nav-text">2404. Most Frequent Even Element</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2405-Optimal-Partition-of-String"><span class="nav-text">2405. Optimal Partition of String</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2406-Divide-Intervals-Into-Minimum-Number-of-Groups"><span class="nav-text">2406. Divide Intervals Into Minimum Number of Groups</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2407-Longest-Increasing-Subsequence-II"><span class="nav-text">Failed: 2407. Longest Increasing Subsequence II</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-311"><span class="nav-text">Weekly Contest 311</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2413-Smallest-Even-Multiple"><span class="nav-text">2413. Smallest Even Multiple</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2414-Length-of-the-Longest-Alphabetical-Continuous-Substring"><span class="nav-text">2414. Length of the Longest Alphabetical Continuous Substring</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2415-Reverse-Odd-Levels-of-Binary-Tree"><span class="nav-text">2415. Reverse Odd Levels of Binary Tree</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2416-Sum-of-Prefix-Scores-of-Strings"><span class="nav-text">Failed: 2416. Sum of Prefix Scores of Strings</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-312"><span class="nav-text">Weekly Contest 312</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2418-Sort-the-People"><span class="nav-text">2418. Sort the People</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2419-Longest-Subarray-With-Maximum-Bitwise-AND"><span class="nav-text">2419. Longest Subarray With Maximum Bitwise AND</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2420-Find-All-Good-Indices"><span class="nav-text">2420. Find All Good Indices</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2421-Number-of-Good-Paths"><span class="nav-text">Failed: 2421. Number of Good Paths</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-312-1"><span class="nav-text">Weekly Contest 312</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2427-Number-of-Common-Factors"><span class="nav-text">2427. Number of Common Factors</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2428-Maximum-Sum-of-an-Hourglass"><span class="nav-text">2428. Maximum Sum of an Hourglass</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2429-Minimize-XOR"><span class="nav-text">2429. Minimize XOR</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2430-Maximum-Deletions-on-a-String"><span class="nav-text">2430. Maximum Deletions on a String</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="hxy"
      src="/images/Robben.gif">
  <p class="site-author-name" itemprop="name">hxy</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">80</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">120</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/huxingyi1997" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;huxingyi1997" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:huxingyi1997@zju.edu.cn" title="E-Mail → mailto:huxingyi1997@zju.edu.cn" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-frog"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">hxy</span>
</div>

<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共1039.2k字</span>
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'pQsO3ySbU4VtWN2j1FLA74Ha-gzGzoHsz',
      appKey     : 'QYacMDY2VY7Wazprg1X6FiUv',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

  
  <!-- 动态背景特效 -->
  <!-- 樱花特效 -->
    <script async src="/js/src/sakura.js"></script>
    <script async src="/js/src/fairyDustCursor.js"></script>
</body>
</html>
